Question:

Actionscript 2.0 script messing up - Movement

by Guest33007  |  earlier

0 LIKES UnLike

//hey guys this is the script im using, the problem with it is it will go to the end of the screen then respawn at the beggining, i want it to go the the right of the screen, then move to the left and repeat its action heres my script

//now this goes on the lasers actions pannel

onClipEvent (load) {

laserMoveSpeed = 20;

this._y = _root.char._y;

this._x = _root.char._x 80;

}

onClipEvent (enterFrame) {

this._x = laserMoveSpeed;

if (this._x>550) {

this._y = _root.respawn._y;

this._x = _root.respawn._x;

laserMoveSpeed = 50;

}

}

//so how would i make it go to x over 550, then if its over that it will start going in reverse direction, then repeat itself (so basicly like this)

- - - - - - - > (OBJECT HERE)

<---------------(OBJECT HERE)

basicly running back and forth on the screen lol

 Tags:

   Report

2 ANSWERS


  1. is this not moving at all? because you need to put an event similiar to the following:

    movieClip._x += 10;

    The plus-equals means that it increments 10 pixels every frame. (The onEnterFrame is required to make it run, which you have already).


  2. I prefer writing Actionscript in one place, in one layer. Actually it&#039;s a good practice to do so. In case you want to know why:

    http://www.adobe.com/devnet/flash/articl...

    Back to your question...

    First make two layers. name the top layer &quot;Actions&quot;. Name the other layer &quot;Laser&quot;.

    Draw the laser in the &quot;Laser&quot; layer. Name the laser movie clip as &quot;laser&quot;. Then leave the layer alone.

    Go to the &quot;Actions&quot; layer. In the Actions panel, write these codes.

    //Firstly, place the laser in the left edge.

    this.onLoad = function() {

    laser._x = 0;

    laser._y = 200;

    }

    //Then set the speed of which the laser will move at.

    var laserSpeed = 20;

    //Then it&#039;s time to move it.

    this.onEnterFrame = function() {

    laser._x += laserSpeed;

    if(laser._x &gt;= 450) {

    laserSpeed = -laserSpeed;

    }

    if(laser._x &lt;= 0) {

    laserSpeed = -laserSpeed;

    }

    }

    EXPLANATIONS:

    laser._x += laserSpeed;

    The + sign is important because it asks the laser to increase its position on every frame. Else, the laser will stay on one spot.

    if(laser._x &gt;= 450) {

    laserSpeed = -laserSpeed;

    }

    If laser passes through x=450 or touches it, makes it move in the opposite direction.

    My document width is 550, but i put 450. The reason is my laser&#039;s width is 100 and its registration point is at top left. So 550 -100 = 450. if i don&#039;t subtract 100, the laser will move until it&#039;s left touches the screen, not its right.

    -laserSpeed will make the laser moves in negative or opposite direction.

    if(laser._x &lt;= 0) {

    laserSpeed = -laserSpeed;

    }

    Same with previous one, except this time the value is 0. I don&#039;t have to subtract 100 because i want the left to touch the screen, and the registration point is already on its left.

    The laser will move to the right until it reaches the right edge of screen, then it will move to the left until it meets the left edge. Then it just repeatedly does that.

    Hope it helps.

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.