We were then set the task of looking through Flash tutorials, to get us back into Flash mode. We had been given some examples that coinsided with the tutorials, which was useful as we could actually see the results of what had been said in the tutorials. I decided to use the coding form the spaceinvaders and pong examples where an object follows the mouse in order to effect its x position. I wanted to add to this and use the mouse to control both the x and y coordinates of the object. My inital idea would be a shooting game where the gun can be manuvered using the mouse and then on click a shot can be fired. I also wanted to introduce a moving target into the animation, so that the user has to aim at, adding another level to the shooting.
Here is how i put it together including the code that i used and an explanation of what it means.
The first stage was to import the background onto the stage. I decided to use a bar scene as the background, as many other shooting games do. The first step was to introduce a gun, i simply cut out an image from the internet to use. Following converting the image into a movie clip i need to make it fire when the mouse was clicked.
onClipEvent (mouseDown) {
_root.gun_mc.play();
}
This code means that when the event mousedown the flash file will return to the main level of the timeline (_root) then look into the Movie clip called gun_mc and consequently fire.
I wanted to introduce a crosshair so that the user can use this to aim where they shoot. i applied the following code to the crosshair's script, which would also set the gun to follow the crosshair.
onClipEvent (load) {
startDrag("", true);
Mouse.hide();
}
onClipEvent (enterFrame) {
_root.gun_mc._x = _root.aim._x;
_root.gun_mc._y = _root.aim._y;
}
I wanted to add a moving target into the game, i decided to use a simply circle to illustrate this, obviously this could be changed to any shape or image. After drawing the circle and converting it into a movie clip i used the following code to make it move across the screen.
onClipEvent (enterFrame) {
if (this._x <= 500) {
this._x += 10;
}
else {
this._x = 30;
}
}
This code checks if the circles x cordinant is less than or equal to 500. If it is then the target is moved 10 px to the right. If its not then move the target to the point on the x axis where x is 30.The number 10 could be changed to a higher number to make it move quicker and a lower number to make it move slower across the stage.
As you can see this was quite a simply animation to put together and didn't involve that much coding, but it acted as a good refresher into using Flash again.
No comments:
Post a Comment