Help - Search - Members - Calendar
Full Version: Movement in actionscript
Pixel2Life Forum > Help Section > Adobe Flash
darkson01
hey how is everyone?

i have a movieclip that has an image in it.

i can make the movieclip "move" to the right. now i want it to go the opposite direction when the x co-ordinate gets to a certain amount. i done this little bit of actionscipt, but it's not working propely. can anyone tell me whats wrong with it?
CODE
onClipEvent (load) {
this.speed = 1;
this._x = this._x-speed;
this.distance = 1;
this.distance1 = -1227.0;
}
onClipEvent (enterFrame) {
if (this._x=this.distance1) {
this._x += speed;
}
if (this._x=this.distance) {
this._x -= speed;
}
}

thanks
Pax
I'll give you a couple tips that should help your animation with code. First thing, rather than having a speed variable, you should have something like velocity or momentum (I'm not a physics guy so I don't know which is the technical term, or if either one is actually right) and always add it to your x/y. Positive velocity moves to the right on the x axis and down on the y axis. Negative velocity moves the opposite directions.

You'll have to think about how to modify the velocity var, because if you use a simple if statement like you have above, it'll keep changing direction on you once the condition is met/not met again.

Hope that helps get you in the right direction. You should try googling actionscript physics or momentum.
darkson01
ok i looked into velocity, and was able to make the images move to the right using this code:
CODE

onClipEvent (load) {
xVelocity = -2;
xPos = _x;
}
onClipEvent (enterFrame) {
xPos += xVelocity;
_x = xPos;
}


the problem i'm having now is to actually make like a boundary for it to reach! i try using a if statement but that screws the whole thing up! how could i would a write a condition for when it reaches a certain position on the x axis it will invert the xVelocity's value?
Pax
My first suggestion is to put the code inside the movie clip rather than on it. Better practice to do so, and I find it easier to work with.

CODE
var xVelocity:Number;
var xMax:Number;
var xMin:Number;
function init(){
    xVelocity = -2;
    xMax = Stage.width;
    xMin = 0;
    
    this.onEnterFrame = function(){
        animate();
    }
}

function animate(){
    if(this._x + this._width >= xMax){
        this._x = xMax - this._width;
        xVelocity *= -1;
    } else if(this._x <= xMin){
        this._x = xMin;
        xVelocity *= -1;
    }
    this._x += xVelocity;
}

init();


I cleaned up the code a bit. You didn't need the xpos var, because it is the same as _x. Multiplying your velocity by -1 will always reverse it (a neg number times a neg number = positive number; positive * negative = negative)

Have a look at it.
darkson01
ok it's not jumping from one end to the other in a split second!
how do i make it slowly move?

all i did was change xMax to 1227.3 and xMin to 2.5.
Pax
So you're saying the ball just flies across to the other side of the screen when it touches the wall? You probably have hte xmin and xmax vars messed up? I checked the code out and everything ran fine on my machine. Just look to make sure you have no typos or anything like that.
darkson01
well actually the whole movie clip is bigger than the stage. i want it to stop moving when the right edge of the movie clip is at the right edge of the stage. im using the x co-ordinates to decied when that is.
Pax
You'll have to take a look at the math and try to piece together what is happening. I'll take a guess and assume the xmin variable is not low enough. If your mc is larger than the stage, figure out what negative x value you want to act as the left "wall" to bounce off of.
darkson01
actually, i forgot to say im using the newest flash with actionscript, it's saying to use something else than "_x" so maybe thats the problem, even when i switch it to actionscript 1.0 & 2.0 it still says it. do you think thats what's not making it work?
Pax
If you're using AS3, you'll need to strip all of the underscores. And this.onEnterFrame won't work anymore either. I suggest you visit the Adobe website and look for some of the AS2 -> AS3 migration documents so you can update the code.
darkson01
ok, i have made a bit of coding that makes a ball move to the right, slows down just before 500, stops at 500, then goes to the left. can anyone help me add to the code so it will slow down and stop at a position on the left hand side of the screen (say 100) and then go to the right again?

here's the code:

CODE
onClipEvent (load) {
this.speed = 2;
this.distance = 500;
}
onClipEvent (enterFrame) {
if (this._x<this.distance) {
this._x += speed;
}
if (this._x>=(this.distance*0.8)) {
speed -= 0.05;
}
if (speed<0) {
speed = -2;
}
}
Stevogame
I have a movieclip, I put this code, but it says: 1087: Syntax error: extra characters found after end of program.

CODE

onClipEvent (load) {
// Choose a movespeed
moveSpeed = 10;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.RIGHT)) {
if (_root.Walls.hitTest(getBounds(_root).xMax, _y, true)) {
} else {
this._x += moveSpeed;
}
// Move Right
} else if (Key.isDown(Key.UP)) {
if (_root.Walls.hitTest(_x, getBounds(_root).yMin, true)) {
} else {
this._y -= moveSpeed;
}
// Move Up
} else if (Key.isDown(Key.DOWN)) {
if (_root.Walls.hitTest(_x, getBounds(_root).yMax, true)) {
} else {
this._y += moveSpeed;
}
// Move Down
} else if (Key.isDown(Key.LEFT)) {
if (_root.Walls.hitTest(getBounds(_root).xMin, _y, true)) {
} else {
this._x -= moveSpeed;
}
// Move Left
}
}
onClipEvent (enterFrame) {
if (_root.player._x > 500) {
this._x -= 10;
tellTarget ("_root.walls") {
_x -= 10;
}
}
if (_root.player._x < 50) {
this._x += 10;
tellTarget ("_root.walls") {
_x += 10;
}
}
if (_root.player._y > 350) {
this._y -= 10;
tellTarget ("_root.walls") {
_y -= 10;
}
}
if (_root.player._y < 50) {
this._y += 10;
tellTarget ("_root.walls") {
_y += 10;
}
}
}
blueskycyber
Hi guy! You can find and download resources on your demand from the following address:

----->>> blueskycyber.com <<<-----

What we have: hotnews, design, Graphics, ebooks, download resources, cracks, serials, keygens, softwares, wallpapers and much more...
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.