advertisement

Hi, this is an extension to drawing with actionscript. In this tutorial I will be teaching you how to add user-controlled movement to your as-drawn movieclip.

Ok, first thing to do is open up the actionsc panel (and do not close it). Now underneath the code that you wrote in the previous tutorial add this line of code:
[as]
onEnterFrame = function() {
[/as]
All that does is defines a function, so when the movie enters this particular frame, it calls the function and runs the code in the brace ( { ).

Now we need to allow the movement, so add this code to the actions:
[as]
if(Key.isDown(Key.LEFT)){
square._x -= 5;
}
[/as]
All this does is checks if the left arrow key is down, and if it is, make the object with the instance name of square (the object we drew in as) to move 5px to the left.
Now we repeat that code but changing Key.LEFT to Key.RIGHT, Key.DOWN and Key.Up and we change square._x -= 5 to: square._x += 5;, square._y -= 5; and square._y += 5; :
[as]
if(Key.isDown(Key.DOWN)){
square._y += 5;
}
if(Key.isDown(Key.UP)){
square._y -= 5;
}
if(Key.isDown(Key.RIGHT)){
square._x += 5;
}
[/as]
and now we close the function defination:
[as]
};
[/as]

That concludes the tutorial. Just to recap:
The full code from this tutorial:
[as]
onEnterFrame = function() {
if(Key.isDown(Key.LEFT)){
square._x -= 5;
}
if(Key.isDown(Key.DOWN)){
square._y += 5;
}
if(Key.isDown(Key.UP)){
square._y -= 5;
}
if(Key.isDown(Key.RIGHT)){
square._x += 5;
}
};
[/as]
and the full code from this tutorial and the previous one is:
[as]
stop();

_root.createEmptyMovieClip(“square”, square.getNextHighestDepth());
square.beginFill(000000, 100);
square.lineStyle(4, 333333, 100);
square.moveTo(0, 265);
square.lineTo(0, 265);
square.lineTo(20, 265);
square.lineTo(20, 285);
square.lineTo(0, 285);

onEnterFrame = function() {
if(Key.isDown(Key.LEFT)){
square._x -= 5;
}
if(Key.isDown(Key.DOWN)){
square._y += 5;
}
if(Key.isDown(Key.UP)){
square._y -= 5;
}
if(Key.isDown(Key.RIGHT)){
square._x += 5;
}
};
[/as]

Thanks for reading,
mortisimus
http://www.flashdoom.net

advertisement