Hey, I’m currently using denacioust’s ActionScript for character movement, where the player is centered on the screen and only the environment moves. I’m wondering how I can allow for the player to move around within a set area, and once he touches the perimeter, then the background starts moving.
So, for instance, say the entire screen area is 500x500, and the area where I want the player to move without the environment moving is centered at 300x300. Once the player’s X or Y coordinates are outside of that designated area, I want him to then remain within the box and the background to scroll as if he were still walking.
I’m not exactly sure how to do it myself, so if anyone can help, it’d be appreciated.
Also, I have a problem with my character’s movement animation. In the code below, you can see that when a directional key is pressed, it gotoAndPlays the appropriate fram within the character, which has the animation for the movement. This works fine for Up, DOWN, LEFT, and RIGHT, but for some reason, the animation doesn’t play on any of the diagonal directions, and I’m sure that I used the same method.
If anyone can find the error within the code, or can offer an alternate technique, it’ be greatly appreciated.
For my character…
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
gotoAndPlay(35);
}
if (Key.isDown(Key.LEFT)) {
gotoAndPlay(25);
}
if (Key.isDown(Key.UP)) {
gotoAndPlay(5);
}
if (Key.isDown(Key.DOWN)) {
gotoAndPlay(15);
}
if (Key.isDown(Key.RIGHT) & Key.isDown(Key.UP)) {
gotoAndPlay(45);
}
if (Key.isDown(Key.LEFT) & Key.isDown(Key.UP)) {
gotoAndPlay(55);
}
if (Key.isDown(Key.RIGHT) & Key.isDown(Key.DOWN)) {
gotoAndPlay(65);
}
if (Key.isDown(Key.LEFT) & Key.isDown(Key.DOWN)) {
gotoAndPlay(75);
}
// check if player hits the buildings
if (_root.floor.map.hitTest(getBounds(_root).xMax, _y, true)) {
_root.floor._x += _root.paceSpeed;
}
if (_root.floor.map.hitTest(getBounds(_root).xMin, _y, true)) {
_root.floor._x -= _root.paceSpeed;
}
if (_root.floor.map.hitTest(_x, getBounds(_root).yMax, true)) {
_root.floor._y += _root.paceSpeed;
}
if (_root.floor.map.hitTest(_x, getBounds(_root).yMin, true)) {
_root.floor._y -= _root.paceSpeed;
}
if (_root.floor.map.hitTest(getBounds(_root).xMax, _y, true)) {
_root.enter._x += _root.paceSpeed;
}
if (_root.floor.map.hitTest(getBounds(_root).xMin, _y, true)) {
_root.enter._x -= _root.paceSpeed;
}
if (_root.floor.map.hitTest(_x, getBounds(_root).yMax, true)) {
_root.enter._y += _root.paceSpeed;
}
if (_root.floor.map.hitTest(_x, getBounds(_root).yMin, true)) {
_root.enter._y -= _root.paceSpeed;
}
if (_root.floor.map.hitTest(getBounds(_root).xMax, _y, true)) {
_root.top._x += _root.paceSpeed;
}
if (_root.floor.map.hitTest(getBounds(_root).xMin, _y, true)) {
_root.top._x -= _root.paceSpeed;
}
if (_root.floor.map.hitTest(_x, getBounds(_root).yMax, true)) {
_root.top._y += _root.paceSpeed;
}
if (_root.floor.map.hitTest(_x, getBounds(_root).yMin, true)) {
_root.top._y -= _root.paceSpeed;
}
}
And for my background…
onClipEvent (enterFrame) {
// Movement
if (Key.isDown(Key.DOWN)) {
_y -= _root.paceSpeed;
}
if (Key.isDown(Key.UP)) {
_y += _root.paceSpeed;
}
if (Key.isDown(Key.LEFT)) {
_x += _root.paceSpeed;
}
if (Key.isDown(Key.RIGHT)) {
_x -= _root.paceSpeed;
}
}
Again, any help is appreciated.