PDA

View Full Version : Moving problem



Tsukimaru
December 23rd, 2007, 09:39 PM
I'm having trouble programming the walking sequence for my character. This is what I want:

When you press the down key, he moves down 48 pixels, but only one pixel at a time, so even if you release the down key before he moves all 48 pixels, he keeps moving until he's gone the entire length.

This is because I'm making a tile based game, and I don't want him to be able to stop in between tiles (my tiles are 48 x 48 pixels).

Can anyone explain how to do this? I tried using a for loop, but I'm not good at those and it didn't work out.

Tsukimaru
December 24th, 2007, 01:27 AM
I found a similar question (http://board.flashkit.com/board/archive/index.php/t-473814.html) on FlashKit, asking about tile by tile movement, but this code doesn't work. Can anyone help?

rahul_7star
December 24th, 2007, 01:35 AM
I found a similar question (http://board.flashkit.com/board/archive/index.php/t-473814.html) on FlashKit, asking about tile by tile movement, but this code doesn't work. Can anyone help?


use for loop and in b/w use hitTest for d block (i=0 to i<block.lenght)

Tsukimaru
December 24th, 2007, 01:57 AM
I'm sorry, that doesn't really help. Like I said, I'm bad with for loops, and for the rest I don't know what you're saying.

Tsukimaru
December 24th, 2007, 02:11 AM
http://www.triquitips.com/flash_tile_movement.html
This is exactly what I'm looking for, but I can't get this code to work either.

Tsukimaru
December 24th, 2007, 04:13 AM
I may have figured it out, can someone tell me if this is efficient enough? With only a little more work, I might be able to turn it into a function that I could use multiple times.
sprite.dir = "down";
sprite.speed = 2;
sprite.steps = (48/sprite.speed);
sprite.moving = false;
sprite.count = 0;
sprite.dead = 0;
sprite.onEnterFrame = function() {
if ((Key.isDown(Key.LEFT)) && (!this.moving)) {
dir = "left";
this.moving = true;
}
if ((Key.isDown(Key.RIGHT)) && (!this.moving)) {
dir = "right";
this.moving = true;
} else if ((Key.isDown(Key.UP)) && (!this.moving)) {
dir = "up";
this.moving = true;
} else if ((Key.isDown(Key.DOWN)) && (!this.moving)) {
dir = "down";
this.moving = true;
}
if (this.moving) {
this.moving = true;
if (this.count>=this.steps) {
this.moving = false;
this.count = 0;
} else {
switch (dir) {
case "left" :
this._x -= this.speed;
if(this.count<7 || this.count>16){
this.gotoAndStop("left");
}else{
this.gotoAndStop("left2");
}
break;
case "right" :
this._x += this.speed;
if(this.count<7 || this.count>16){
this.gotoAndStop("right");
}else{
this.gotoAndStop("right2");
}
break;
case "up" :
this._y -= this.speed;
if(this.count<7 || this.count>16){
this.gotoAndStop("up");
}else{
this.gotoAndStop("up2");
}
break;
case "down" :
this._y += this.speed;
if(this.count<7 || this.count>16){
this.gotoAndStop("down");
}else{
this.gotoAndStop("down2");
}
break;
}
this.count++;
}
}
};