PDA

View Full Version : Movement using buttons and actionscript



rhombusleech
September 8th, 2003, 09:27 PM
I know this is something very simple to do, but I'm just having actionscript writers block. I've done searches here for what I'm looking for but haven't found quite what I want. Here's what I want to do

I have a movie clip on the stage, say a ball. I also have two buttons, a left and right arrow. When I mouse over the left arrow I want the ball to move left, and vice versa for the right arrow. Also I want to the ball to stop moving when it reaches the edge of the stage, in either direction.

Thanks in advance for the help.

rysolag
September 8th, 2003, 09:42 PM
//put this in the ball movie clip, give it an instance name of ball_mc

onClipEvent(load)
{
left = false;
right = false;
speed = 5;
}

onClipEvent(enterFrame)
{
if(left)
_x = _x - speed;

if(right)
_x = _x + speed;
}

//make the left arrow and right arrow movie clips and put this in them

//left
on(rollOver)
{
_root.ball_mc.left = true;
}

on(rollOut)
{
_root.ball_mc.left = false;
}

//right
on(rollOver)
{
_root.ball_mc.right = true;
}

on(rollOut)
{
_root.ball_mc.right = false;
}

rysolag
September 8th, 2003, 09:48 PM
//for keeping it on stage, assuming _x is the center of the ball

onClipEvent(enterFrame)
{
if(left && (_x - width/2) > 0)
_x = _x - speed;

if(right && (_x +width/2) < 550)
_x = _x + speed;
}

rhombusleech
September 8th, 2003, 09:51 PM
Where does the code to keep it on stage go?

Thanks a bagillion rysolag!!

rysolag
September 9th, 2003, 01:17 AM
hey,

that onClipEvent(enterFrame) goes in the ball movie clip.
It was an update to the one I gave you the first time.

Voetsjoeba
September 9th, 2003, 12:13 PM
Or another way ... this goes on the main timeline:

leftbutton.onRollOver = function() {
_root.ball.onEnterFrame = function() {
this._x -= 2;
if (this._x<=20) {
delete this.onEnterFrame;
}
};
};
leftbutton.onRollOut = function() {
delete _root.ball.onEnterFrame;
};
rightbutton.onRollOver = function() {
_root.ball.onEnterFrame = function() {
this._x += 2;
if (this._x>=400) {
delete this.onEnterFrame;
}
};
};
rightbutton.onRollOut = function() {
delete _root.ball.onEnterFrame;
};