Animation stop actionscript 3.0 tick - actionscript-3

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.

Related

How to add score when collecting things (Actionscript 3 Adobe Animate)?

How to create an incremented score after he pickup the rubbish? I am very new on this thing and need help on coding.
My code below is on a separated .as file (movieclip).
As you can see, there are codes for when the player hittestobject, the "rubbish" will go away but how to apply a new code for score to that specific function? so the score will be incremented each time he pick up the rubbish.
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class mazeClass extends MovieClip {
var mouseUp:Boolean = false;
var mouseDown:Boolean = false;
var mouseLeft:Boolean = false;
var mouseRight:Boolean = false;
var speed:Number = 5;
public function mazeClass() {
// constructor codea
stage.addEventListener(KeyboardEvent.KEY_DOWN, CheckDownKeys);
stage.addEventListener(KeyboardEvent.KEY_UP, CheckUpKeys);
stage.addEventListener(Event.ENTER_FRAME, updatePos);
}
private function CheckDownKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =true;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =true;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =true;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =true;
}
}
private function CheckUpKeys(e:KeyboardEvent){
if(e.keyCode == Keyboard.UP){
mouseUp =false;
}
if(e.keyCode == Keyboard.DOWN){
mouseDown =false;
}
if(e.keyCode == Keyboard.LEFT){
mouseLeft =false;
}
if(e.keyCode == Keyboard.RIGHT){
mouseRight =false;
}
}
private function updatePos(e:Event){
if(mouseUp == true){
if(!wall.hitTestPoint(jacob.x,jacob.y-22, true)){
jacob.y -= speed;
}
}
if(mouseDown == true){
if(!wall.hitTestPoint(jacob.x, jacob.y+22, true)){
jacob.y += speed;
}
}
if(mouseLeft == true){
if(!wall.hitTestPoint(jacob.x-22, jacob.y, true)){
jacob.x -= speed;
}
}
if(mouseRight == true){
if(!wall.hitTestPoint(jacob.x+22, jacob.y, true)){
jacob.x += speed;
}
}
if(jacob.hitTestObject(a1)){
a1.x = a1.y = -1000;
_root.score = 0;
_root.score++;
score.text = _root.score;
}
if(jacob.hitTestObject(a2)){
a2.x = a2.y = -1000;
}
if(jacob.hitTestObject(a3)){
a3.x = a3.y = -1000;
}
if(jacob.hitTestObject(a4)){
a4.x = a4.y = -1000;
}
}
}
}

AS3 hitTestObject is not working

I was making flash that moving a circle on my keyboard and creating a flash that hit the wall.
But method 'hitTestObject' is not working...
Hou to solve this ploblem?
import flash.events.KeyboardEvent;
import flash.events.Event;
stage.addEventListener(KeyboardEvent.KEY_DOWN,atKeydown);
stage.addEventListener(KeyboardEvent.KEY_UP,atKeyup);
function atKeydown(e :KeyboardEvent) :void
{
if(e.keyCode == 38) stage.addEventListener(Event.ENTER_FRAME,enterYUp);
if(e.keyCode == 40) stage.addEventListener(Event.ENTER_FRAME,enterYDown);
if(e.keyCode == 37) stage.addEventListener(Event.ENTER_FRAME,enterXLeft);
if(e.keyCode == 39) stage.addEventListener(Event.ENTER_FRAME,enterXRight);
}
function atKeyup(e :KeyboardEvent) :void
{
if(e.keyCode == 38) stage.removeEventListener(Event.ENTER_FRAME,enterYUp);
if(e.keyCode == 40) stage.removeEventListener(Event.ENTER_FRAME,enterYDown);
if(e.keyCode == 37) stage.removeEventListener(Event.ENTER_FRAME,enterXLeft);
if(e.keyCode == 39) stage.removeEventListener(Event.ENTER_FRAME,enterXRight);
}
function enterYUp(e :Event) :void
{
ball.y -= 10;
}
function enterYDown(e :Event) :void
{
ball.y += 10;
}
function enterXLeft(e :Event) :void
{
ball.x -= 10;
}
function enterXRight(e :Event) :void
{
ball.x += 10;
}
if(ball.hitTestObject(wall) == true) trace("!!!!");
function enterYUp(e :Event) :void
{
var bool = checkHit("y", -10); // each frame checks for a hit, returns a Boolean (see the function below)
carryOut(bool, "y", -10); // the movement is carried out based on the Boolean
}
function enterYDown(e :Event) :void
{
var bool = checkHit("y", +10); // you can send the "y" property, and where it wants to go.
carryOut(bool, "y", +10);
}
function enterXLeft(e :Event) :void
{
var bool = checkHit("x", -10);
carryOut(bool, "x", -10);
}
function enterXRight(e :Event) :void
{
var bool = checkHit("x", +10);
carryOut(bool, "x", -10);
}
function checkHit(_prop, _num):Boolean
{
// This single function will run every frame, comparing the balls 'predicted position'
if((ball[_prop] + _num) <= _numCheck)
{
// i might use hitTestPoint instead, but I hate using hitTestObject... it can act weird sometimes.
return true;
}
else
{
return false;
}
}
function carryOut(bool, _prop,_num):void
{
if(bool)
{
trace("hit");
}
else
{
// if the Boolean returned false
// ball[_prop] is ball.x or ball.y depending on if you sent "x" or "y".
// it will ADD the _num you sent, so adding -10 is the same as subtracting.
ball[_prop] += _num;
}
}

Issue with Flash AS3 Player movieclip and creating an instance

So heres my issue, I created a movieclip class within Adobe Animate (Warmage.as) and I added it to my stage (addChild(char)). I tried to access the properties says undefined property of char. But I created a class for Warmage and created an instance of it (char).
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.Timer;
public class Main_class extends MovieClip
{
//player stats
var hsp:Number = 0;
var vsp:Number = 0;
var floor:Number = 1318;
var attackCounter = 5;
var doubleJumpCount = 0;
//Player states
var rightSide:Boolean = false;
var rDown:Boolean = false;
var lDown:Boolean = false;
var jumped:Boolean = false;
var onGround:Boolean = false;
var crouchMode:Boolean = false;
var attackMode:Boolean = false;
var canDoubleJump = false;
public function Main_class()
{
var char:Warmage = new Warmage();//Adds player to the level
char.x = 500;
char.y = 300;
addChild(char);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);//Stage listens no matter what
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
}
function gameLoop(e:Event):void
{
if(rDown)
{
char.x += 10;
}
if(lDown)
{
char.x -= 10;
}
}
function keyPressed(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
rDown = true;
}
if(e.keyCode == Keyboard.LEFT)
{
lDown = true;
}
if(e.keyCode == Keyboard.UP && onGround)
{
jumped = true;
//doubleJumpCount += 1;
}
if(e.keyCode == Keyboard.DOWN && onGround)
{
crouchMode = true;
}
if(e.keyCode == Keyboard.SPACE && onGround)
{
attackMode = false;
}
}
function keyReleased(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
rDown = false;
}
if(e.keyCode == Keyboard.LEFT)
{
lDown = false;
}
if(e.keyCode == Keyboard.UP)
{
jumped = true;
}
if(e.keyCode == Keyboard.DOWN)
{
crouchMode = false;
}
if(e.keyCode == Keyboard.SPACE)
{
attackMode = true;
}
}
}
}
Because you're using a local variable.
public function Main_class()
{
var char:Warmage = new Warmage();//Adds player to the level
trace(char); // OK. You can available char inside of this function.
}
function gameLoop(e:Event):void
{
trace(char); // You can not available that variable here.
}
Use global variable. Declare a variable outside a function.
private var char:Warmage;
public function Main_class()
{
char = new Warmage();//Adds player to the level
char.x = 500;
char.y = 300;
addChild(char);
trace(char); // OK
}
function gameLoop(e:Event):void
{
trace(char); // OK
}
Please read "Understanding variable scope"
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9d.html

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...
}
}

Some Problems with my AS3 code

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;
}
}