Creating a Game - Step 2


Creating a Game - Step 2

This tutorial continues on from the previous one.
In this tutorial I will show you how to check for collisions and how to stop the character from going outside the screen.

If you done as instructed in the last tutorial, you should have a nice and beautiful character that walks around :p
Make sure you have your document opened.

2). Now check to make sure you canvas size is 550 by 400...
TutorialNinja Image

If its not, change it.

3). Now select the frame on layer 1 as shown...
TutorialNinja Image

Open up the Actions panel...
TutorialNinja Image

Your original code should be there from the previous tutorial.
Replace the code with...

Flash ActionScript
  1. var speed:Number = 10; //SET A VARIABLE FOR THE SPEED
  2.  
  3. character.onEnterFrame = function() { //ON ENTER FRAME
  4.  
  5. if (Key.isDown(Key.RIGHT)){ //IF SOMEONE PRESSES DOWN RIGHT
  6. this._x += speed; //MOVE BY 10 ON THE X AXIS
  7. } //END OF STATEMENT
  8. if (Key.isDown(Key.LEFT)){ //IF SOMEONE PRESSES DOWN LEFT
  9. this._x -= speed; //MOVE BY -10 ON THE X AXIS
  10. } //END OF STATEMENT
  11. if (Key.isDown(Key.UP)){ //IF SOMEONE PRESSES DOWN UP
  12. this._y -= speed; //MOVE BY -10 ON THE Y AXIS
  13. } //END OF STATEMENT
  14. if (Key.isDown(Key.DOWN)){ //IF SOMEONE PRESSES DOWN
  15. this._y += speed; //MOVE BY 10 ON THE Y AXIS
  16. } //END OF STATEMENT
  17.  
  18. if (this._x > Stage.width){ //IF X POSITION IS MORE THAN THE CANVAS WIDTH
  19. this._x = 0; //SET THE X POSITION OF THE CHARACTER TO 0
  20. }else if (this._x < 0){ //OTHERWISE IF THE X POSITION IS LESS THAN 0
  21. this._x = Stage.width; //SET THE X POSITION TO THE END OF THE X AXIS OF THE CANVAS
  22. } //END STATEMENT
  23. if (this._y > Stage.height){ //IF THE Y POSITION IS MORE THAN THE CANVAS HEIGHT
  24. this._y = 0; //SET THE CHARACTERS Y POSITION TO 0
  25. }else if (this._y < 0){ //OTHERWISE IF THE Y POSITION IS LESS THAN 0
  26. this._y = Stage.height; //SET THE Y POSITION OF THE CHARACTER TO THE END OF THE Y AXIS
  27. } //END OF STATEMENT
  28.  
  29. } //END THE 'ON ENTER FRAME'


As you can see, i have added on a part at the end which will check whether the character has crossed the boundaries.
And if they have they will be sent to an appropriate place.
Read the comments for more info.

Now you can test your movie clip.
You should be able to go anywhere you like without going off the screen forever.

4). Now to do a collision. What we are going to do is add a massive black block to the side.
Select the rectangle tool.
TutorialNinja Image

Set the properties of the rectangle to the following...
TutorialNinja Image

Then draw the rectangle like i drew mine...
TutorialNinja Image

Select the rectangle...
TutorialNinja Image

Press F8, a dialog box should appear.
TutorialNinja Image

Use the settings i used.
Okay now, carring on... with your rectangle selected change the instance name to bigwall
TutorialNinja Image

With that done, lets do the collision check.
Click on the frame 1 on layer 1...
TutorialNinja Image

Open up the Actions panel...
TutorialNinja Image

Now replace the code in the Actions panel with the updated code below...
Flash ActionScript
  1. //ACTIONSCRIPT 2 IS BEING USED
  2.  
  3. character.onEnterFrame = function() { //ON ENTER FRAME
  4. var speed:Number = 10; //SET A VARIABLE FOR THE SPEED
  5.  
  6. if (Key.isDown(Key.RIGHT)){ //IF SOMEONE PRESSES DOWN RIGHT
  7. this._x += speed; //MOVE BY 10 ON THE X AXIS
  8. } //END OF STATEMENT
  9. if (Key.isDown(Key.LEFT)){ //IF SOMEONE PRESSES DOWN LEFT
  10. this._x -= speed; //MOVE BY -10 ON THE X AXIS
  11. } //END OF STATEMENT
  12. if (Key.isDown(Key.UP)){ //IF SOMEONE PRESSES DOWN UP
  13. this._y -= speed; //MOVE BY -10 ON THE Y AXIS
  14. } //END OF STATEMENT
  15. if (Key.isDown(Key.DOWN)){ //IF SOMEONE PRESSES DOWN
  16. this._y += speed; //MOVE BY 10 ON THE Y AXIS
  17. } //END OF STATEMENT
  18.  
  19. if (this._x > Stage.width){ //IF X POSITION IS MORE THAN THE CANVAS WIDTH
  20. this._x = 0; //SET THE X POSITION OF THE CHARACTER TO 0
  21. }else if (this._x < 0){ //OTHERWISE IF THE X POSITION IS LESS THAN 0
  22. this._x = Stage.width; //SET THE X POSITION TO THE END OF THE X AXIS OF THE CANVAS
  23. } //END STATEMENT
  24. if (this._y > Stage.height){ //IF THE Y POSITION IS MORE THAN THE CANVAS HEIGHT
  25. this._y = 0; //SET THE CHARACTERS Y POSITION TO 0
  26. }else if (this._y < 0){ //OTHERWISE IF THE Y POSITION IS LESS THAN 0
  27. this._y = Stage.height; //SET THE Y POSITION OF THE CHARACTER TO THE END OF THE Y AXIS
  28. } //END OF STATEMENT
  29.  
  30. if (this.hitTest(_root.bigwall)){ //IF CHARACTER HITS THE BIG WALL
  31. this._x -= speed; //MAKE IT STOP COMLPETELY
  32. } //END STATEMENT
  33.  
  34. } //END THE 'ON ENTER FRAME'


Again ive commented it to help you.
Well now you can try running your movie. If its correct then it should work without flaw.


I get an actionscript error when I try to test it
Check to see if the code on your Actions panel matches the code that i have provided.

What will you be doing in Step 3 of this tutorial?
Well step 3 will be a little more complicated.
I have not decided what I am going to do for Step 3 but please,
if you want something done please issue a request in the comments section.
However i will definately need to fix the bug that when you walk all the way to the left...
you can go through the wall on the other side. Ill fix that in the next tutorial.

I want an answer which is not here...
Post a comment if you would like to know anything else or want to report any bugs etc.

Thanks for reading this tutorial.
Please do report any bugs found.
Step 3 will probably come later on. Well, when i decide what to do for the next tutorial.

If you would like me to do something for the next tutorial please post a comment.
ilyas-shezad's Avatar
Views:
3,309
Rating:
Posted on Monday 11th August 2008 at 12:29 PM
Posted on Wednesday 30th July 2008 at 10:29 AM
Posted on Wednesday 30th July 2008 at 10:27 AM
ilyas-shezad
ilyas-shezad's Avatar
Hey everyone.
I havnt had any requests yet, and i figured most of this tutorials views must be coming from another site.
So if you have a request but dont want to sign up, you can email me at hotmail_is@hotmail.co.uk
Thanks!

ps. I was thinking of making the next tutorial about going into different maps or giving ur character attributes such as health (plus with something dangerous on the screen which damages your players health).