Skip to main content
Known Participant
September 22, 2011
Answered

help.

  • September 22, 2011
  • 1 reply
  • 1807 views

Hi, i am having an very weird problem in as3:

i am having 2 different codes in 1 scene, this should be ok i think, but only 1 of the 2 codes are working,

i am having 1 code for scrolling background.

and 1 code for moving an animation with arrowkeys ( keyboardevent )

but only the scrolling background is working, and when i am removing the scrolling background code, the keyboardevent is working perfect.

I am only in able to use 1 code, the other is not working, but both codes are correct, i tried to add gotoAndStop, but that didn't work too.

when i have both codes in as3, my animation ( with the keyboardevent ) disappears, it is not in the scene anymore.

does anyone knows how to fix it??

code:

var goDown:Boolean = false;

var goUp:Boolean = false;

var goLeft:Boolean = false;

var goRight:Boolean = false;

function hearKeyDown(yourEvent:KeyboardEvent):void{   

if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   

if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = true; }      

if (yourEvent.keyCode==Keyboard.UP){ goUp = true; } 

if (yourEvent.keyCode==Keyboard.DOWN){ goDown = true; }

stage.addEventListener(Event.ENTER_FRAME, moveCube);

}

function hearKeyUp(yourEvent:KeyboardEvent):void{    

if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   

if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = false; }      

if (yourEvent.keyCode==Keyboard.UP){ goUp = false; } 

if (yourEvent.keyCode==Keyboard.DOWN){ goDown = false; }

stage.removeEventListener(Event.ENTER_FRAME, moveCube);

}

function moveCube(evt:Event):void {

if (goRight){ cube_mc.x+=5 };   

if (goLeft){ cube_mc.x-=5 };  

if (goUp){ cube_mc.y-=5 };  

if (goDown){ cube_mc.y+=5 };

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);

stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

cube_mc.gotoAndStop("albertj");

var scrollSpeed:uint = 1;

//This adds two instances of the movie clip onto the stage.

var s1:ScrollBg = new ScrollBg();

var s2:ScrollBg = new ScrollBg();

addChild(s1);

addChild(s2);

//This positions the second movieclip next to the first one.

s1.x = 0;

s2.x = s1.width;

//The speed of the scroll movement.

//Adds an event listener to the stage.

stage.addEventListener(Event.ENTER_FRAME, moveScroll);

//This function moves both the images to left. If the first and second

//images goes pass the left stage boundary then it gets moved to

//the other side of the stage.

function moveScroll(e:Event):void{

s1.x -= scrollSpeed; 

s2.x -= scrollSpeed; 

if(s1.x < -s1.width){

s1.x = s1.width;

}else if(s2.x < -s2.width){

s2.x = s2.width;

}

}

This topic has been closed for replies.
Correct answer kglad

ok, thanks, it is not yet fully solved but it helped me, because the cube_mc is now in my scene but it is behind my background, so the cube_mc is moving behind the background, how can i make the cube_mc moving in the front of my scene so the background is moving behind the cube_mc?


use:

var goDown:Boolean = false;

var goUp:Boolean = false;

var goLeft:Boolean = false;

var goRight:Boolean = false;

function hearKeyDown(yourEvent:KeyboardEvent):void{   

if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   

if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = true; }      

if (yourEvent.keyCode==Keyboard.UP){ goUp = true; } 

if (yourEvent.keyCode==Keyboard.DOWN){ goDown = true; }

//stage.addEventListener(Event.ENTER_FRAME, moveCube);

}

function hearKeyUp(yourEvent:KeyboardEvent):void{    

if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   

if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = false; }      

if (yourEvent.keyCode==Keyboard.UP){ goUp = false; } 

if (yourEvent.keyCode==Keyboard.DOWN){ goDown = false; }

//stage.removeEventListener(Event.ENTER_FRAME, moveCube);

}

function moveCube(evt:Event):void {

if (goRight){ cube_mc.x+=5 };   

if (goLeft){ cube_mc.x-=5 };  

if (goUp){ cube_mc.y-=5 };  

if (goDown){ cube_mc.y+=5 };

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);

stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

cube_mc.gotoAndStop("albertj");

var scrollSpeed:uint = 1;

//This adds two instances of the movie clip onto the stage.

var s1:ScrollBg = new ScrollBg();

var s2:ScrollBg = new ScrollBg();

addChild(s1);

addChild(s2);

addChild(cube_mc);

//This positions the second movieclip next to the first one.

s1.x = 0;

s2.x = s1.width;

//The speed of the scroll movement.

//Adds an event listener to the stage.

stage.addEventListener(Event.ENTER_FRAME, moveScroll);

//This function moves both the images to left. If the first and second

//images goes pass the left stage boundary then it gets moved to

//the other side of the stage.

function moveScroll(e:Event):void{

moveCube(e);

s1.x -= scrollSpeed; 

s2.x -= scrollSpeed; 

if(s1.x < -s1.width){

s1.x = s1.width;

}else if(s2.x < -s2.width){

s2.x = s2.width;

}

}

1 reply

kglad
Community Expert
Community Expert
September 22, 2011

use:

var goDown:Boolean = false;

var goUp:Boolean = false;

var goLeft:Boolean = false;

var goRight:Boolean = false;

function hearKeyDown(yourEvent:KeyboardEvent):void{   

if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = true; }   

if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = true; }      

if (yourEvent.keyCode==Keyboard.UP){ goUp = true; } 

if (yourEvent.keyCode==Keyboard.DOWN){ goDown = true; }

//stage.addEventListener(Event.ENTER_FRAME, moveCube);

}

function hearKeyUp(yourEvent:KeyboardEvent):void{    

if (yourEvent.keyCode==Keyboard.RIGHT){ goRight = false; }   

if (yourEvent.keyCode==Keyboard.LEFT){ goLeft = false; }      

if (yourEvent.keyCode==Keyboard.UP){ goUp = false; } 

if (yourEvent.keyCode==Keyboard.DOWN){ goDown = false; }

//stage.removeEventListener(Event.ENTER_FRAME, moveCube);

}

function moveCube(evt:Event):void {

if (goRight){ cube_mc.x+=5 };   

if (goLeft){ cube_mc.x-=5 };  

if (goUp){ cube_mc.y-=5 };  

if (goDown){ cube_mc.y+=5 };

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, hearKeyDown);

stage.addEventListener(KeyboardEvent.KEY_UP, hearKeyUp);

cube_mc.gotoAndStop("albertj");

var scrollSpeed:uint = 1;

//This adds two instances of the movie clip onto the stage.

var s1:ScrollBg = new ScrollBg();

var s2:ScrollBg = new ScrollBg();

addChild(s1);

addChild(s2);

//This positions the second movieclip next to the first one.

s1.x = 0;

s2.x = s1.width;

//The speed of the scroll movement.

//Adds an event listener to the stage.

stage.addEventListener(Event.ENTER_FRAME, moveScroll);

//This function moves both the images to left. If the first and second

//images goes pass the left stage boundary then it gets moved to

//the other side of the stage.

function moveScroll(e:Event):void{

moveCube(e);

s1.x -= scrollSpeed; 

s2.x -= scrollSpeed; 

if(s1.x < -s1.width){

s1.x = s1.width;

}else if(s2.x < -s2.width){

s2.x = s2.width;

}

}

PilsjeBoyAuthor
Known Participant
September 22, 2011

I replaced your code, with my code, but no difference, my cube_mc still disappears.

kglad
Community Expert
Community Expert
September 22, 2011

do you mean you see your cube_mc move off-stage?