Some Problems with my AS3 code - actionscript-3

Some troubles to make my new flash game(i used AS3,CS5.5):
I just try to make my character(hatt) walk and jump, but I can't make it at the same time, well, idk how... Furthermore, I don't know how make my character recognize the ground.
And at last this thing here:
"Scene 1, Layer 'hatt', Frame 1, Line 6 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)."
Plz help me and give some hints plz... Thanks.
Here's the code:
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
function update(evt:Event):void
{
if (jumping)
{
hatt.y += jumpPow;
jumpPow += grav;
if (hatt.y >= stage.stageHeight)
{
jumping = false;
hatt.y = stage.stageHeight;
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
function myFunction (event: KeyboardEvent){
if(event.keyCode == Keyboard.LEFT) {
hatt.x -= 5
}
if(event.keyCode == Keyboard.RIGHT) {
hatt.x += 5
}
}

remove this line:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
remove function onKeyDown:
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
replace myFunction with this:
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
hatt.x -= 5;
if(event.keyCode == Keyboard.RIGHT)
hatt.x += 5;
if(event.keyCode == Keyboard.UP && jumping != true)
{
jumpPow = -25;
jumping = true;
}
}

Related

AS3 Restrict ship in visible stage area

Hey so i've been making a space shooter game and i created the movement but i'm trying to restricting the ship to the visible stage area and i can't figure out how to do that
also i'm sorry if my code it's really messy i'm really new at this
the stage size it's 1920 x 1080
here's the code of the movement
var leftKeyPressed: Boolean = false;
var rightKeyPressed: Boolean = false;
var upKeyPressed: Boolean = false;
var downKeyPressed: Boolean = false;
var xMoveSpeed: Number = 15;
var yMoveSpeed: Number = 15;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
function keyDownHandler(e: KeyboardEvent): void
{
if (e.keyCode == Keyboard.LEFT)
{
leftKeyPressed = true;
}
if (e.keyCode == Keyboard.RIGHT)
{
rightKeyPressed = true;
}
if (e.keyCode == Keyboard.UP)
{
upKeyPressed = true;
}
if (e.keyCode == Keyboard.DOWN)
{
downKeyPressed = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
function keyUpHandler(e: KeyboardEvent): void
{
if (e.keyCode == Keyboard.LEFT)
{
leftKeyPressed = false;
}
if (e.keyCode == Keyboard.RIGHT)
{
rightKeyPressed = false;
}
if (e.keyCode == Keyboard.UP)
{
upKeyPressed = false;
}
if (e.keyCode == Keyboard.DOWN)
{
downKeyPressed = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, moveHero);
function moveHero(e: Event): void
{
if (leftKeyPressed)
{
ship.x -= xMoveSpeed;
}
if (rightKeyPressed)
{
ship.x += xMoveSpeed;
}
if (upKeyPressed)
{
ship.y -= yMoveSpeed;
}
if (downKeyPressed)
{
ship.y += yMoveSpeed;
}
}
Well, you can start with this.
// One map is much better than a separate variable for each and every key.
var keyMap:Object = new Object;
// Initial keymap values.
keyMap[Keyboard.UP] = 0;
keyMap[Keyboard.DOWN] = 0;
keyMap[Keyboard.LEFT] = 0;
keyMap[Keyboard.RIGHT] = 0;
// With the keymap you don't need these big event handlers,
// you need a single one with one line of code.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onUpDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUpDown);
function onUpDown(e:KeyboardEvent):void
{
// The members of the object with the names of key codes
// will be set to 1 in case of KEY_DOWN event or 0 in case of KEY_UP.
keyMap[e.keyCode] = int(e.type == KeyboardEvent.KEY_DOWN);
}
stage.addEventListener(Event.ENTER_FRAME, moveHero);
// This will provide you with min and max values for both X and Y coordinates.
// You will probably need to adjust it because the ship have width and height.
var conStraints:Rectangle = new Rectangle(0, 0, 1920, 1080);
var xMoveSpeed:Number = 15;
var yMoveSpeed:Number = 15;
function moveHero(e:Event):void
{
var anX:Number = ship.x;
var anY:Number = ship.y;
// Use keymap to figure the new coordinates.
// Take into account all the relevant keys pressed.
anX += xMoveSpeed * (keyMap[Keyboard.RIGHT] - keyMap[Keyboard.LEFT]);
anY += yMoveSpeed * (keyMap[Keyboard.DOWN] - keyMap[Keyboard.UP]);
// Now set the new ship coordinates with the regard to constraints.
ship.x = Math.min(conStraints.right, Math.max(conStraints.left, anX));
ship.y = Math.min(conStraints.bottom, Math.max(conStraints.top, anY));
}
I think this is the easiet solution, tell me if it worked or not
function keyDownHandler(e: KeyboardEvent): void
{
if (e.keyCode == Keyboard.LEFT && ship.x > 15)
{
leftKeyPressed = true;
}
if (e.keyCode == Keyboard.RIGHT && ship.x < 1905)
{
rightKeyPressed = true;
}
if (e.keyCode == Keyboard.UP && ship.y > 15)
{
upKeyPressed = true;
}
if (e.keyCode == Keyboard.DOWN && ship.y < 1065)
{
downKeyPressed = true;
}
}

Flash Game Actionscript-3 Output Error #1009

I am making my own game using Actionscript 3 for coding in Flash. The game is about a Runner Character just like Super Mario and Stickyman, who just run, jump, dies.. and so on..
After being very organized by dividing each window of the game in a different Scene, I was still having the problem of executing the piece of code "gotoAndStop()".. That's why I decided to make it much simple by using only one Scene, but each wind of the game in a different frame in the Main Timeline. So I worked on the root, but still getting the same problem that stuck in a white screen!
Anyways, one of the main problem that I am facing here is the Error #1009. Even when I tried to read other topics with the same issue but I didn't find the answer that suites my case.
This is the Error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at HR4_fla::MainTimeline/movePlayer()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Apple/update()
Here is the Code:
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.geom.Rectangle;
stop();
var KeyThatIsPressed:uint;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var score:int = 0;
var lives:int = 3;
player_mc.health = 100;
player_mc.dead = false;
//var TouchRestartBox:Boolean = false;
var playerSpeed:Number = 8;
var gravity:Number = 2;
var yVelocity:Number = 0;
var canJump:Boolean = false;
var canDoubleJump: Boolean = false;
//var appleCount:int;
stage.addEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
function cameraFollowCharacter(event:Event):void
{
scrollRect = new Rectangle(player_mc.x - stage.stageWidth/2, player_mc.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight);
}
//PressKey function here
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;
}
}
//ReleaseKey function here
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;
}
}
//stage.addEventListener(Event.ENTER_FRAME, GameOver);
stage.addEventListener(Event.ENTER_FRAME, movePlayer);
function movePlayer(event:Event):void
{
if(!rightKeyIsDown && !leftKeyIsDown && !upKeyIsDown)
{
player_mc.gotoAndStop(1);
}
if(rightKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x+= playerSpeed;
player_mc.scaleX = 0.59;
}
if(leftKeyIsDown)
{
player_mc.gotoAndStop(2);
player_mc.x-= playerSpeed;
player_mc.scaleX = -0.59;
}
if(upKeyIsDown && canJump)
{
player_mc.gotoAndStop(3);
yVelocity = -15;
canJump = false;
canDoubleJump = true;
}
if(upKeyIsDown && canDoubleJump && yVelocity > -2)
{
yVelocity = -13;
canDoubleJump = false;
}
yVelocity +=gravity;
if(!floor_mc.hitTestPoint(player_mc.x,player_mc.y, true))
{
player_mc.y+=yVelocity;
}
if(yVelocity > 20)
{
yVelocity =20;
}
for(var i:int=0; i<10; i++)
{
if(floor_mc.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.y--;
yVelocity = 0;
canJump = true;
}
}
for(var j:int=0; j<=2; j++)
{
if(rb.hitTestPoint(player_mc.x, player_mc.y, true))
{
player_mc.x = -1703.35;
player_mc.y = 322.1;
player_mc.scaleX = 0.59;
lives = lives - 1;
}
if(lives == 0)
{
// GameOver();
// remove all the event listeners
// stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
gotoAndStop(124);
//(root as MovieClip).gotoAndStop(124);
}
}
// appleCount_txt.text = "Apples:" + appleCount;
}
/*function GameOver()
{
// lives = 3;
// remove all the event listeners
stage.removeEventListener(Event.ENTER_FRAME, GameOver);
stage.removeEventListener(Event.ENTER_FRAME, movePlayer);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, PressAKey);
stage.removeEventListener(KeyboardEvent.KEY_UP, ReleaseAKey);
stage.removeEventListener(Event.ENTER_FRAME, cameraFollowCharacter);
// player_mc.stop();
gotoAndStop(1); // this has your "dead" screen on it.
}*/
Can anyone help me solving this problem please?
Thanks
There's a listener still getting executed after the frame change. In that handler, an object is referenced that does not exist on that frame and is thus null.
Changing frames only provides visual changes, not necessarily a complete change of state.

How do i play a moveiclip from the library with a key press

im trying to play a moveiclip from the library with a key press and i have as linked it but it still does not work (this is only a bit of the code there are event listeners and handlers in place and the character moves fine)
stage.addEventListener(Event.ENTER_FRAME, Move);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
var aP:Boolean = false;
var dP:Boolean = false;
//creating a new Character_right instance
var character_right:Character_right = new Character_right();
function Move(vet:KeyboardEvent)
{
if(aP)
{
char.x -= 5;
char.scaleX = -0.55;
}
if(dP)
{
char.x += 5;
char.scaleX = -0.55;
}
}
function keyPress(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = true;
break;
}
case Keyboard.D:
{
dP = true;
character_right.play();
break;
}
}
}
function keyUp(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = false;
break;
}
case Keyboard.D:
{
dP = false;
break;
}
}
}
In your code, you are executing the function Character_right.play() after break and break stops the rest of the loop code from being executed, so execute Character_right.play() before break, for example:
function keyPress(evt:KeyboardEvent)
{
switch(evt.keyCode)
{
case Keyboard.A:
{
aP = true;
break;
}
case Keyboard.D:
{
dP = true;
Character_right.play();
break;
}
}
}
Check this post, with a nice tutorial about keyboard events, I believe will be important for you to understand the concept and implement on your logic.
Briefly it's something like the following example, but remember about to implement all necessary logic for your scope.
var isRight:Boolean;
var isLeft:Boolean;
var isUp:Boolean;
var isDown:Boolean;
//creating a new Character_right instance
var character_right:Character_right = new Character_right();
stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeyHandler, false, 0, true);
function downKeyHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT )
{
isRight = true;
}
if (event.keyCode == Keyboard.LEFT)
{
isLeft = true;
}
if (event.keyCode == Keyboard.UP)
{
isUp = true;
}
if (event.keyCode == Keyboard.DOWN)
{
isDown = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, upKeyHandler, false, 0, true);
function upKeyHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT)
{
isRight = false;
}
if (event.keyCode == Keyboard.LEFT)
{
isLeft = false;
}
if (event.keyCode == Keyboard.UP)
{
isUp = false;
}
if (event.keyCode == Keyboard.DOWN)
{
isDown = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, loopHandler, false, 0, true);
function loopHandler(event:Event):void
{
if (isRight)
{
//do something hereā€¦
character_right.play();
}
if (isLeft)
{
//do something here...
}
if (isUp)
{
//do something here...
}
if (isDown)
{
//do something here...
}
}

Animation stop actionscript 3.0 tick

So, I have this game and I want the player to move, so I use a tick method when the key is down it creates 2 event listeners, a keyup listener and a tick.The keyup listener removes its self and removes the tick.The tick contains the movement and where I presumed the animation.
Here is my code:
// For every frame //
import flash.events.Event;
import flash.events.TouchEvent;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
var gravity = 0.6;
var floor = 410;
jordan1.y = floor;
jordan1.speedY = floor;
jordan1.impulsion = 10;
var onFloor:Boolean = false;
var keyRight:Boolean = false;
stage.addChild(jordan1);
stage.addChild(leftwall1);
stage.addChild(finish1);
stage.addChildAt(abovedoor, 0);
stage.addChild(left);
stage.addChild(right);
stage.addChild(jump);
addEventListener(Event.ENTER_FRAME, enters);
function enters(e:Event) {
jordan1.speedY += gravity;
jordan1.y += jordan1.speedY
if(jordan1.y > floor) {
jordan1.speedY = 0;
jordan1.y = floor;
onFloor = true;
if(keyRight == true) {
jordan1.gotoAndPlay(7);
}
if(keyRight == false) {
jordan1.gotoAndStop(1);
}
}
}
function stageup(e:MouseEvent) {
removeEventListener(Event.ENTER_FRAME,tickleft);
removeEventListener(Event.ENTER_FRAME,tickright);
removeEventListener(MouseEvent.MOUSE_UP, stageup);
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, stagedown);
function stagedown(e:MouseEvent) {
stage.addEventListener(MouseEvent.MOUSE_UP, stageup);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, jumpkey);
function jumpkey(e:KeyboardEvent) {
if(e.keyCode == Keyboard.SPACE) {
if(onFloor == true)
{
jordan1.speedY=-jordan1.impulsion;
onFloor = false;
jordan1.gotoAndStop(2);
}
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, leftkey);
function leftkey(e:KeyboardEvent) {
if(e.keyCode == Keyboard.LEFT) {
addEventListener(Event.ENTER_FRAME, leftkeytick);
stage.addEventListener(KeyboardEvent.KEY_UP, leftkeydown);
}
}
function leftkeydown(e:KeyboardEvent) {
removeEventListener(Event.ENTER_FRAME, leftkeytick);
stage.removeEventListener(KeyboardEvent.KEY_UP, leftkeydown);
}
function leftkeytick(e:Event) {
jordan1.x-=10;
if(onFloor == true) {
jordan1.gotoAndStop(3);
}
if(onFloor == false) {
jordan1.gotoAndStop(6);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, rightkey);
function rightkey(e:KeyboardEvent) {
if(e.keyCode == Keyboard.RIGHT) {
keyRight == true;
addEventListener(Event.ENTER_FRAME, rightkeytick);
stage.addEventListener(KeyboardEvent.KEY_UP, rightkeydown);
}
}
function rightkeydown(e:KeyboardEvent) {
removeEventListener(Event.ENTER_FRAME, rightkeytick);
keyRight == false;
stage.removeEventListener(KeyboardEvent.KEY_UP, rightkeydown);
}
function rightkeytick(e:Event) {
jordan1.x+=10;
if(onFloor == true) {
jordan1.gotoAndPlay(7);
}
if(onFloor == false) {
jordan1.gotoAndStop(5);
}
}
// Just for this frame //
leftwall1.addEventListener(Event.ENTER_FRAME, leftwall1hit);
function leftwall1hit(e:Event) {
if(leftwall1.hitTestObject(jordan1)) {
jordan1.x +=10;
}
}
abovedoor.addEventListener(Event.ENTER_FRAME, abovedoorhit);
function abovedoorhit(e:Event) {
if(abovedoor.hitTestObject(jordan1)) {
jordan1.x-=10;
}
}
jordan1.addEventListener(Event.ENTER_FRAME, finish1hit);
function finish1hit(e:Event) {
if(jordan1.x > 960) {
gotoAndStop(67);
}
}
So, when I test it and put the keydown it goestoandstops at 7, instead of playing.Please help! Thank you!
Your goto shouldn't be in your tick, or they will be executed each frame. Which explain why it's staying on frame 7.
Instead put them in your KEY_DOWNlistener :
animated = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, rightkey);
function rightkey(e:KeyboardEvent) {
if(e.keyCode == Keyboard.RIGHT) {
if(onFloor == true && !animated) {
jordan1.gotoAndPlay(7);
}
if(onFloor == false && !animated) {
jordan1.gotoAndStop(5);
}
animated = true;
addEventListener(Event.ENTER_FRAME, rightkeytick);
stage.addEventListener(KeyboardEvent.KEY_UP, rightkeydown);
}
}
function rightkeydown(e:KeyboardEvent) {
animated = false;
removeEventListener(Event.ENTER_FRAME, rightkeytick);
stage.removeEventListener(KeyboardEvent.KEY_UP, rightkeydown);
}
function rightkeytick(e:Event) {
jordan1.x+=10;
}
But you shouldn't use a tick function with a keydown event.
KEY_DOWNis called each frame while you're pressing the/a key.
If you can't change your logic (which is not good for what you want to achieve) you could flag your handlers.
By having a boolean for each key you're listening to.
And then in your tick, do what you need to do depending on what keys are down.
keyRight = false;
keyLeft = false;
//... same for all your keys.
// or you could store them in one object for a more dynamic solution.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
stage.addEventListener(Event.ENTER_FRAME,tick);
function keyDown(e:KeyboardEvent){
if(e.keyCode === Keyboard.RIGHT && !keyRight){
keyRight = true;
}
else if(e.keyCode === Keyboard.LEFT && !keyLeft){
keyLeft = true;
}
//... same for all your keys.
}
function keyUp(e:KeyboardEvent){
if(e.keyCode === Keyboard.RIGHT){
keyRight = false;
}
else if(e.keyCode === Keyboard.LEFT){
keyLeft = false;
}
//... same for all your keys.
}
function tick(e:Event){
if(keyRight) jordan1.x += 10;
if(keyLeft) jordan1.x -= 10;
//... etc for all your keys.
}
This other question cover the subject of multiple KEY_DOWN pretty well.

how do I either stop the tween at the point of my choice (first code)? or how do I stop the easing (second code)?

This is for Actionscript 3.0 on CS5.
I've been having some issues with fl.transitions.easing.None.easeNone. I tried to animate my MovieClip character without easing and stopping on a dime. I used the following code to stop the easing:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT && var_move == false)
{
var_move = true;
hero.gotoAndStop("Walk");
hero.scaleX = 1;
var tween1:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x+75, 15, false)
tween1.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
}
if(evt.keyCode == Keyboard.LEFT && var_move == false)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = -1;
var tween2:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x-75, 15, false)
tween2.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
}
}
function onMotionFinished($evt:TweenEvent):void
{
hero.gotoAndPlay("Stand");
var_move = false;
}
Which it did but if you just tap Right or Left Arrow the MovieClip character will not stop on that frame. It continues until the tween is done. If I use the following code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
function keyPressed(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = 1;
hero.x += 5;
}
if(evt.keyCode == Keyboard.LEFT)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = -1;
hero.x -= 5;
}
}
function releaseKey(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT || evt.keyCode == Keyboard.LEFT)
{
hero.gotoAndStop("Stand");
}
}
the MovieClip character eases into the walk animation, but stops where I want it to. how do I either stop the tween at the point of my choice (first code) or how do I stop the easing (second code)?
For tweening, I highly recommend using the library at http://greensock.com ... the free TweenMax/TweenLite will save you tons of time trying to solve problems like this... they already did the hard part :)