PDA

View Full Version : Complex Background Movement


ArcaneMX
04-13-2005, 01:28 PM
Hi, simple question here;

My game includes a charecter with this script

onClipEvent(enterFrame){
speed *= .8;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
_x += x;
_y += y;
}
}
onClipEvent(enterFrame){
if(Key.isDown(Key.UP)) {
speed -= 1;
} else {if(Key.isDown(Key.DOWN)){
speed += 1;
} else {if(Key.isDown(Key.LEFT)){
_rotation -= 5;
} else {if(Key.isDown(Key.RIGHT)){
_rotation += 5;
}
}
}
}
}
(The character begins facing downward)

I need the background to;
1. Move as the character moves, and
2. DO NOT ROTATE. I cannot have a rotating background for gameplay
reasons.

To accomplish this, I need a script to either
1. Detect angle char is facing and move along that line,
or
2. Create a similar script that will move itself, and still be accurate, NOT
rotating.

Thnaks guys

ski_la
04-13-2005, 07:20 PM
Hi,

If you want to make the background appear to move with the character then you need to change it's _x and _y values along with the characters' own, e.g.


onClipEvent(enterFrame){
speed *= .8;
/*
create a value to allow us to do "parallax" scrolling (the background does not move as fast as it is further away)
*/
parallax = 0.7;
x = Math.sin(_rotation*(Math.PI/180))*speed;
y = Math.cos(_rotation*(Math.PI/180))*speed*-1;
if (!_root.land.hitTest(_x+x, _y+y, true)) {
/*Move the background in the same direction as the character use "-" for opposite direction */
_parent.backg._x += x * parallax;
_parent.backg._y += y * parallax;
_x += x;
_y += y;

}
}

ArcaneMX
04-14-2005, 01:23 AM
Thank you!
Im glad you can help me, but I still have a few questions;

My character is one mc, and my background is another, which one did you intend I placed the script on? (the character is called "char" and the background is called "bkg").

If you need to mess around with it, create any mc and add my script in my first post to it. The code works on any mc.

ski_la
04-14-2005, 08:31 PM
Hi,

You can control both mc's from the same script, I assumed the onEnterFrame bit I changed and posted was for the character - I was changing it's "_x" and "_y" whilst referring to another mc in the parent timeline, which would be my background:

_parent.backg._x += x * parallax;
_parent.backg._y += y * parallax;

For your background, replace _parent.backg with the target path of "bkg".
As long as you know where the background clip is, either relative to the script (above) or absolutely (_level0.etc.etc.bkg), you can control both mc's movement from the same place, this is more efficient than calculating the position in two separate places as you essentially use the same information for both.
You could even add a third mc that is totally responsible for handling keypresses and stuff and controlling anything that needs to be moved.

Hope this helps.

SKI