Movement in actionscript
#1
Posted 12 June 2007 - 02:43 PM
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?
[codebox]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;
}
}[/codebox]
thanks
#2
Posted 13 June 2007 - 08:58 AM
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.
#3
Posted 14 June 2007 - 04:40 AM
[codebox]
onClipEvent (load) {
xVelocity = -2;
xPos = _x;
}
onClipEvent (enterFrame) {
xPos += xVelocity;
_x = xPos;
}
[/codebox]
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?
#4
Posted 14 June 2007 - 11:04 AM
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.
#5
Posted 25 June 2007 - 12:54 PM
how do i make it slowly move?
all i did was change xMax to 1227.3 and xMin to 2.5.
#6
Posted 25 June 2007 - 08:29 PM
#7
Posted 15 July 2007 - 12:39 PM
#8
Posted 15 July 2007 - 09:44 PM
#9
Posted 16 July 2007 - 08:16 AM
#10
Posted 16 July 2007 - 09:22 AM
Edited by Pax, 16 July 2007 - 09:23 AM.
#11
Posted 17 July 2007 - 02:04 AM
here's the code:
[codebox]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;
}
}[/codebox]
Edited by darkson01, 19 July 2007 - 11:03 AM.
#12
Posted 20 December 2008 - 11:53 AM
[codebox]
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;
}
}
}
[/codebox]
#13
Posted 19 February 2009 - 02:44 AM
----->>> blueskycyber.com <<<-----
What we have: hotnews, design, Graphics, ebooks, download resources, cracks, serials, keygens, softwares, wallpapers and much more...
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users