PDA

View Full Version : altering x position of movie clip dynamically



earleybird
May 15th, 2004, 08:58 AM
Hi,

I've built a little experiment which lets the user control a car with the fwd and bckwrd arrow keys. the idea is to move the car fwd but if it goes above a certain speed you have to slow it down. I have it working but the way i've done it is to move the backround to make it look like the car is moving. However, the backround can only be a certain width so atfer a while is moves clear of the screen. I'm trying to have it so that when the road's _x coordinate hits a certain point then it will jump back to its original point and the road will look like its just continuously moving. Here is the main code.

onClipEvent (load){
//dir = 0;
speed = 0;
}
onClipEvent (enterFrame) {
xspeed = Math.cos(dir*Math.PI/180)*speed;
yspeed = Math.sin(dir*Math.PI/180)*speed;
_x += xspeed;
_y += yspeed;
//_rotation = dir;
if (Key.isDown(Key.RIGHT)) {
speed += -1;
}
if (speed<=0){
if (Key.isDown(Key.LEFT)) {
speed += 1;
}
}

if (speed<0) {
_root.caption.carspeed = -1*speed;
}
if (speed>=0){
_root.caption.carspeed = 1*speed;
}

if (speed<12){
_root.caption.health = "SAFE";
elseif (speed>=12)
_root.caption.health = "DANGER";
}

This is the bit where I tried to get the _x value to change:

if(_x==(-3104)){
_x =+ 3104;
}

}

I've attatched my .fla to help explain!

I think i'm totally wrong and would greatly appreciate some help!
:)

SeiferTim
May 15th, 2004, 09:31 AM
onClipEvent (load){
//dir = 0;
speed = 0;
}
onClipEvent (enterFrame) {
xspeed = Math.cos(dir*Math.PI/180)*speed;
yspeed = Math.sin(dir*Math.PI/180)*speed;
if ((_x += xspeed) < movieXMax){
_x += xspeed;
}
_y += yspeed;
//_rotation = dir;
if (Key.isDown(Key.RIGHT)) {
speed += -1;
}
if (speed<=0){
if (Key.isDown(Key.LEFT)) {
speed += 1;
}
}

if (speed<0) {
_root.caption.carspeed = -1*speed;
}
if (speed>=0){
_root.caption.carspeed = 1*speed;
}

if (speed<12){
_root.caption.health = "SAFE";
elseif (speed>=12)
_root.caption.health = "DANGER";
}

}

Try that...

earleybird
May 16th, 2004, 09:06 AM
thanks for trying but unfortunately that doesn't seem to work. I'll keep trying though. If you have anymore ideas i'd be very grateful!:-/
xx

dinner dog
May 21st, 2004, 10:31 AM
OK, I think I did what you wanted. The Clip is now shorter, basically because it was uselessly big when you wanted to wrap. Basically it now works on the "flintstones method" (term coined dinner dog 2004) which works like they run past the same door several times in a few seconds. I also added a pimp-ed out :p car, mainly because it looked cool to pass, it's on a new layer, don't worry.

anyway, the background clip will move until it PASSES a point, checking for exactly a point is rather useless when you have exceleration because you could be going up by tens (or what ever number the current accel is) and the odds of you hitting 3104 going tens is very small if not nill. so now it will check if the clip has passed a point, if so it will wrap back to an earlier _x just in time to allow our car and tree to pass again.

of couse this is really basic, it doesn't wrap when in reverse and it gets a little dull once the same object has passed several times, but you shoulld get the idea. perhaps a random object attatched to the bg every wrap would be cool. or you could change some values and extend the track a bit and add a grandstand or something.

If you have any other questions please post.
good luck.