Jump to content


Photo

Movement in actionscript


  • Please log in to reply
12 replies to this topic

#1 darkson01

darkson01

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Gfx, skateboarding, playing guitar, listening to punk music and flash!

Posted 12 June 2007 - 02:43 PM

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?
[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 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 13 June 2007 - 08:58 AM

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.

#3 darkson01

darkson01

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Gfx, skateboarding, playing guitar, listening to punk music and flash!

Posted 14 June 2007 - 04:40 AM

ok i looked into velocity, and was able to make the images move to the right using this code:
[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 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 14 June 2007 - 11:04 AM

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.

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 darkson01

darkson01

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Gfx, skateboarding, playing guitar, listening to punk music and flash!

Posted 25 June 2007 - 12:54 PM

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.

#6 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 25 June 2007 - 08:29 PM

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.

#7 darkson01

darkson01

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Gfx, skateboarding, playing guitar, listening to punk music and flash!

Posted 15 July 2007 - 12:39 PM

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.

#8 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 15 July 2007 - 09:44 PM

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.

#9 darkson01

darkson01

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Gfx, skateboarding, playing guitar, listening to punk music and flash!

Posted 16 July 2007 - 08:16 AM

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?

#10 Pax

Pax

    P2L Jedi

  • Members
  • PipPipPip
  • 911 posts
  • Gender:Male
  • Location:Windsor, ON, Canada

Posted 16 July 2007 - 09:22 AM

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.

Edited by Pax, 16 July 2007 - 09:23 AM.


#11 darkson01

darkson01

    Young Padawan

  • Members
  • Pip
  • 114 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Gfx, skateboarding, playing guitar, listening to punk music and flash!

Posted 17 July 2007 - 02:04 AM

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:

[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 Stevogame

Stevogame

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 20 December 2008 - 11:53 AM

I have a movieclip, I put this code, but it says: 1087: Syntax error: extra characters found after end of program.

[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 blueskycyber

blueskycyber

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 19 February 2009 - 02:44 AM

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...




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users