PDA

View Full Version : [FMX04] Key.addlistener??



cookie
March 16th, 2004, 07:37 PM
I'm having trouble getting this part of Sen's code to function! I attached his tutorial fla! I've searched in the AS help in Flash and tried modifying the Key.addlistener object but it still won't respond to the arrow keys!

Sen's code

// set the ball to recognize key presses and move based on them
Key.addListener(ball);
ball.onKeyDown = function(){
var xmove, ymove;
if (Key.isDown(Key.UP)){
ymove = 1;
}else if (Key.isDown(Key.DOWN)){
ymove = -1;
}else if (Key.isDown(Key.LEFT)){
xmove = -1;
}else if (Key.isDown(Key.RIGHT)){
xmove = 1;
}
// update new position based on ball's current
xmove += this.x;
ymove += this.y;
if (IsValidTile(xmove, ymove)){
moveToGridSpace(xmove, ymove);
}
}


My attempt

// set the ball to recognize key presses and move based on them
ball = new Object ();
ball.onKeyDown = function(){
var xmove, ymove;
if (Key.isDown(Key.UP)){
ymove = 1;
}else if (Key.isDown(Key.DOWN)){
ymove = -1;
}else if (Key.isDown(Key.LEFT)){
xmove = -1;
}else if (Key.isDown(Key.RIGHT)){
xmove = 1;
}
Key.addListener(ball);
// update new position based on ball's current
xmove += this.x;
ymove += this.y;
if (IsValidTile(xmove, ymove)){
moveToGridSpace(xmove, ymove);
}
}


What is wrong?

cookie
March 17th, 2004, 04:10 PM
Can someone please tell me what's wrong with the script.:hangover:

cookie
March 17th, 2004, 09:03 PM
After two days of waiting for a reply to my problem regarding Senocular's isoexample1.fla. I have despite my limited AS knowledge solved the problem. I guess Sen's "How Actionscript Works" (which I read 30min ago and was mind boggling:h:, lit some lights up there:thumb:)!

I just want to point out Sen that the problem in your AS seemed to be with the declaration of the following variables xmove and ymove. I added that they are equal to 0 and it worked. I searched for more info in the flash help, but I don't understand why you need to set an initial value when defining variables?

Perhaps someone can explain this phenomenon! :rambo:


// set the ball to recognize key presses and move based on them

Key.addListener(ball);

ball.onKeyDown = function(){
//change the following adding equal to 0
var xmove=0, ymove=0;

if (Key.isDown(Key.UP)){

ymove = 1;

}else if (Key.isDown(Key.DOWN)){

ymove = -1;

}else if (Key.isDown(Key.LEFT)){

xmove = -1;

}else if (Key.isDown(Key.RIGHT)){

xmove = 1;

}

// update new position based on ball's current

xmove += this.x;

ymove += this.y;

if (IsValidTile(xmove, ymove)){

moveToGridSpace(xmove, ymove);

}

}