how to do key board events in acrion script - actionscript-3

I have been looking up how to handle keyboard events and I've copied what they had but I still get the errors:
Line 84, Column 25 1120: Access of undefined property Keyboard.
Line 92, Column 25 1120: Access of undefined property Keyboard.
Line 95, Column 25 1120: Access of undefined property Keyboard.
What am I doing wrong?
import flash.events.*; //my event import
addEventListener(KeyboardEvent.KEY_DOWN, controls); // my event listener
//LAUNCHER CONTROLS
public function controls(event: KeyboardEvent):void {
trace(event.keyCode);
trace("hi");
if (event.keyCode == Keyboard.SPACE) {
//CREATE BULLET
bullet = new Bullet(bulletLauncher.x);
bullet.x = bullet.mX;
bullet.y = 449;
addChild(bullet);
}
if (event.keyCode == Keyboard.LEFT) {
}
if (event.keyCode == Keyboard.RIGHT) {
}
}

Related

Error Function Scene Adobe Animate Syntax Error

i tried to use adobe animate 2019 to make a quiz. there are some errors:
Scene 15, Layer 'Actions', Frame 1, Line 68, Column 7 1083: Syntax
error: else is unexpected.
Scene 15, Layer 'Actions', Frame 1, Line 63, Column 47 1084: Syntax error: expecting semicolon before rightparen. Scene 15, Layer
'Actions', Frame 1, Line 62, Column 53 1084: Syntax error: expecting
identifier before logicaland.
Here's my code:
import flash.events.MouseEvent;
//melakukan inisialisasi posisi awal dari object
//fungsi ini dibuat terutama untuk keperluan "me-reset posisi object"
function inisialisasi_posisi():void{ //
benar_mc.visible = false;
salah_mc.visible = false;
//posisi ayah
ayah_mc.x = posisiawal_ayah_mc.x;
ayah_mc.y = posisiawal_ayah_mc.y;
//posisi kakak
kakak_mc.x = posisiawal_kakak_mc.x;
kakak_mc.y = posisiawal_kakak_mc.y;
//posisi ibu
ibu_mc.x = posisiawal_ibu_mc.x;
ibu_mc.y = posisiawal_ibu_mc.y;
//posisi adik
adik_mc.x = posisiawal_adik_mc.x;
adik_mc.y = posisiawal_adik_mc.y;
} //
inisialisasi_posisi();
//DRAG & DROP OBJECT
function dragdrop(object_mc:MovieClip):void
{
object_mc.addEventListener(MouseEvent.MOUSE_DOWN, startdrag);
object_mc.addEventListener(MouseEvent.MOUSE_UP, stopdrag);
}
function startdrag(e:MouseEvent):void
{
e.currentTarget.startDrag();
}
function stopdrag(e:MouseEvent):void
{
e.currentTarget.stopDrag();
}
//set dragdrop
dragdrop(ayah_mc);
dragdrop(kakak_mc);
dragdrop(ibu_mc);
dragdrop(adik_mc);
//result
test_btn.addEventListener(MouseEvent.CLICK, test);
function test(e:MouseEvent):void
{
//jika semua object berada pada tempat yang benar
if(ayah_mc.hitTestObject(ayahTarget_mc)== true &&
kakak_mc.hitTestObject(kakakTarget_mc)== true &&
ibu_mc.hitTestObject(ibuTarget_mc)== true) &&
adik_mc.hitTestObject(adikTarget_mc)== true) ;
{
benar_mc.visible = true;
salah_mc.visible = false;
}
else
{
benar_mc.visible = false;
salah_mc.visible = true;
}
}
//reset object ke posisi awal
reset_btn.addEventListener(MouseEvent.CLICK, reset);
function reset(e:MouseEvent):void
{
inisialisasi_posisi();
}
/* Click to Go to Scene and Play
Clicking on the specified symbol instance plays the movie from the specified scene and frame.
Instructions:
1. Replace "Scene 3" with the name of the scene you would like play.
2. Replace 1 with the frame number you would like the movie to play from in the specified scene.
*/
button_nextquiz1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_16);
function fl_ClickToGoToScene_16(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Scene 16");
}
/* Click to Go to Previous Scene and Play
Clicking on the specified symbol instance moves the playhead to the previous scene in the timeline and continues playback in that scene.
*/
button_previousquiz1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToPreviousScene_17);
function fl_ClickToGoToPreviousScene_17(event:MouseEvent):void
{
MovieClip(this.root).prevScene();
}
stop();
Thank you so much!
You have an extra parentheses and and extra semicolon in the if condition inside your test function. Change it to:
function test(e:MouseEvent):void
{
//jika semua object berada pada tempat yang benar
if(ayah_mc.hitTestObject(ayahTarget_mc) == true &&
kakak_mc.hitTestObject(kakakTarget_mc) == true &&
ibu_mc.hitTestObject(ibuTarget_mc) == true &&
adik_mc.hitTestObject(adikTarget_mc) == true)
{
benar_mc.visible = true;
salah_mc.visible = false;
}
else
{
benar_mc.visible = false;
salah_mc.visible = true;
}
}

Why does flash output a TypeError: Error #2007: Parameter child must be non-null. at flash.display::DisplayObjectContainer/addChild()

This is my code that I have so far
package {
import flash.display.MovieClip;
import flash.events.*;
public class Main extends MovieClip {
var leftpressed:Boolean = false;
var rightpressed:Boolean = false;
public function Main()
{
//constructor code
Spaceship.addEventListener(Event.ENTER_FRAME, moveToKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, setKeyUnpress);
stage.addEventListener(KeyboardEvent.KEY_DOWN, FlyBullet);
}
function moveToKey (event:Event)
{
if (leftpressed && (Spaceship.x>0))
{
Spaceship.x = -5;
}
if (rightpressed && (Spaceship.x<550))
{
Spaceship.x = +5;
}
}
function setKeyPress(e:KeyboardEvent):void
{
if (e.keyCode == 37)
{
leftpressed = true;
}
if (e.keyCode == 39)
{
rightpressed = true;
}
}
function setKeyUnpress(e:KeyboardEvent): void
{
if (e.keyCode == 37)
{
leftpressed = false;
}
if (e.keyCode == 39)
{
rightpressed = false;
}
}
function FlyBullet (e:KeyboardEvent):void
{
if (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
}
}
}
With the biggest issue with the final FlyBullet function that outputs an the error listed as
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/addChild()
at Main/FlyBullet()
This is your problem:
if (e.keyCode == 32)
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
There are no curly brackets so the if condition only applies to the first line where bulletshot is assigned. In other words, if you press any key other than SPACE it will skip the assignment of bulletshot but still try to addChild(bulletshot), but since bulletshot was not assigned a value it is null and you get errors.
I think you meant this:
if (e.keyCode == Keyboard.SPACE) {
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y
}
PS: Your code is poorly indented in a lot of places. It helps to have correctly indented code. An auto-formatter could help you here.
It is generally considered bad practice to omit brackets in multiline if statements, this is an example why. Whenever e.keyCode != 32 the addChild line will try to run.
Solution would be:
///...in the Main function//
stage.addEventListener(KeyboardEvent.KEY_DOWN, flyBullet);
///...
function flyBullet (e:KeyboardEvent):void
{
if (e.keyCode == 32)
{
var bulletshot:Bullet = new Bullet();
addChild(bulletshot);
bulletshot.x = Spaceship.x;
bulletshot.y = Spaceship.y;
}
}
Some unrelated as3 code quality tips, to avoid possible errors later:
I'm guessing Spaceship isn't a static class, so i suggest using lowercase first letters for variable/function names, and uppercase for classes, but this is just good practice and does not affect your codes correctness.
It is also good practice to define the accessibility of your functions, i.e public, private, internal, protected. If you omit it the default is internal.
And only skip brackets after conditionals when necessary.

What is the error causing my character delay its response when press a key?

I have some problems with my character's walking command. It delays its movement for a bit before it actually moves. And then at times it completely ignores the command to stop walking when I released the key.
Code:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
hero.gotoAndStop(1);
var rightPressed: Boolean = new Boolean(false);
var leftPressed: Boolean = new Boolean(false);
var upPressed: Boolean = new Boolean(false);
var downPressed: Boolean = new Boolean(false)
var heroSpeed: Number = 10;
var keys: Array = [];
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
function keyDownHandler(KeyEvent: KeyboardEvent): void {
if (keys[Keyboard.RIGHT]) {
rightPressed = true;
} else if (keys[Keyboard.LEFT]) {
leftPressed = true;
} else if (keys[Keyboard.DOWN]) {
downPressed = true;
} else if (keys[Keyboard.UP]) {
upPressed = true;
}
}
function keyUpHandler(KeyEvent: KeyboardEvent): void {
if (keys[Keyboard.RIGHT]) {
rightPressed = false;
hero.gotoAndStop(4)
} else if (keys[Keyboard.LEFT]) {
leftPressed = false;
hero.gotoAndStop(2)
} else if (keys[Keyboard.DOWN]) {
downPressed = false;
hero.gotoAndStop(1);
} else if (keys[Keyboard.UP]) {
upPressed = false;
hero.gotoAndStop(3);
}
}
function gameLoop(loopEvent: Event): void {
if (rightPressed) {
hero.x += heroSpeed;
hero.gotoAndStop(8)
}
if (leftPressed) {
hero.x -= heroSpeed;
hero.gotoAndStop(6)
}
if (downPressed) {
hero.y += heroSpeed;
hero.gotoAndStop(5);
}
if (upPressed) {
hero.y -= heroSpeed;
hero.gotoAndStop(7);
}
}
function onKeyDown(e: KeyboardEvent): void {
keys[e.keyCode] = true;
}
function onKeyUp(e: KeyboardEvent): void {
keys[e.keyCode] = false;
}
Warnings:
Scene 1, Layer 'Actions', Frame 1, Line 68, Column 10 Warning: 1090: Migration issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyDown', callback_handler).
Scene 1, Layer 'Actions', Frame 1, Line 72, Column 10 Warning: 1090: Migration issue: The onKeyUp event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyUp', callback_handler).
You should get rid of the two extra KeyboardEvent handlers (onKeyUp and onKeyDown), and move the code you have there into keyUpHandler and keyDownHandler. That will solve those migration warnings (because onKeyUp and onKeyDown were special methods in AS2), and it may be the solution to your other problem: I guess sometimes the onKeyDown handler gets executed after keyDownHandler, which means the boolean values in your array are not set yet and no movement will start.
Even better: also get rid of the array with booleans (and keys! you're abusing an Array for Dictionary use) and do it like this:
function keyDownHandler(KeyEvent: KeyboardEvent): void {
if (event.keyCode==Keyboard.RIGHT]) {
rightPressed = true;
}
//etc
}

AS3 keyboard animated character - Compiler errors all over the place despite following tutorial to the letter

Trying to create a 2D character that responds to keyboard inputs in Flash Professional CC. Every YouTube tutorial my son and I have tried produces Compiler errors and NOTHING works.
import flash.events.KeyboardEvent;
import flash.events.Event;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var playerSpeed:int = 7;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseAKey);
function pressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
function releaseAKey(event.KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function moveThePlayer(event:Event):void
{
if(rightKeyIsDown == true)
{
player_mc.x += playerSpeed;
}
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
}
What is wrong with the above code? We have followed this tutorial, which seems very straightforward however the minute we type in a curly bracket it highlights in red, and then when we try to test we receive Compiler Errors.
Scene 1, Layer 'Player', Frame 1, Line 35, Column 27 1084: Syntax error: expecting rightparen before dot.
We are using Flash Professional CC and AS3 - is there a better resource for tutorials for this type of thing as we're being driven mad having tried at least 5 of these now without success.
Would really appreciate some advice on why the above is wrong and also where we might find tutorials that actually work!
Thank you.
NJ & Son! :)
I threw your code into Flash and did not see any issues surrounding mismatching parenthesis; however, I did notice the following errors
First, In the following if control structure:
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
You are attempting to use an operator of -+ but no such operator exists in Flash ActionScript. I'm thinking you meant to do the following instead:
if(leftKeyIsDown == true)
{
player_mc.x -= playerSpeed;
}
Second, the issue is right here:
function releaseAKey(event.KeyboardEvent):void
{
// your code
}
You need a : to specify the datatype. Can't believe I missed that. So it should be:
function releaseAKey(event:KeyboardEvent):void
{
// your code
}
I was able to plug this code into Flash CC and get it to compile with no errors. Cheers!

Strange issue with preloader

I have some problem with my preloader.
Preloader Code:
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
var game:MovieClip
var added:Boolean;
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("source.swf");
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest);
function onCompleteHandler(e:Event):void {
game = e.currentTarget.content
game.alpha = 0;
}
function onProgressHandler(e:ProgressEvent):void {
loader.loadBar.setProgress(e.bytesLoaded, e.bytesTotal);
}
addEventListener(Event.ENTER_FRAME, function(e:Event):void {
if(game != null){
if(!added) {
addChild(game);
added = true;
}
if(game.alpha < 1) game.alpha += 0.1;
When I load my game console returns TypeError: Error #1009: Cannot access a property or method of a null object reference.
I turn on permit debugging in game and again load. Now console returns TypeError: Error #1009: Cannot access a property or method of a null object reference.
at main()[C: \Users\Lukasz\Desktop\Flash\rs\main.as:141];
So I checked 141 line and since 141 to 155 I have keyboard events.
stage.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void {
if(e.keyCode == 32 && moveAvailable) {
startEvent();
}else if(e.keyCode == 32) {
moveAvailable = true;
}
moveSpeed = 70;
});
stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void {
if(e.keyCode == 32) {
moveSpeed = 140
if(!startBtn.enb) moveAvailable = false;
}
});
When I get comment /**/ between this code game load correctly.
By the way I try this.parent and parent. instead of stage. but nothing changed :(
Someone have idea on this problem ?
You need check stage before use it
if (stage) {
addStageEvent();
} else {
this.addEventListener(Event.ADDED_TO_STAGE, addStageEvent);
}
function addStageEvent(e:Event = null):void {
//put the 141-155 line code here
}