Simple Gaming
|
Top Previous Next |
1. | Open the "dragging.swi" file used in the Mouse Dragging tutorial and save the file as "mygame.swi"
|
2. | Draw a rectangle to define the playing area. Name the rectangle "boundary" and tick the 'Target' checkbox
|
3. | Rename the ball Object "bat" and change its Event scripts to be the following:
|
As the scripting is becoming more complex, it is useful to add comments using Add Script | Debugging | Comments. Comments are shown as a line starting with // or surrounded by /* */.
|
The onLoad() function is used to declare variables and setup initial values.
|
The onSelfEvent(press) is used to toggle the dragging of the bat Object. Press the left mouse button once on the 'bat' object to start dragging. Press again to stop dragging. Note that the original onSelfEvent(release) has been deleted.
|
The onEnterFrame() function is called at the start of each Frame. This function is used to calculate and store the instantaneous speed of the bat. The speed is calculated by saving the number of pixels that the Object has moved since the function was previously called. The current position is then saved in x0, y0 for use when the function is called again on the next Frame.
|
|
Note that in the image below, only a subsection of the script is shown.
|
4. | Use the Circle Tool to create a new circle. Colour the circle blue. The diameter of this circle was 40
|
5. | Use the Menu function Modify | Grouping | Group as Sprite to change the unnamed shape into a Sprite. Name the sprite "ball". Using a Sprite for the ball allows multiple balls to be added later using Instances
|
6. | Add the following script to the 'ball' Sprite. The script below contains some hyperlinks to assist with understanding.
|
Again, many comments have been used to describe the script. To save time they can be omitted, but they do help explain the code.
|
|
For this Object the onLoad() function is used to declare and initialise variables. dx and dy are used to hold the Object's velocity. These are initialised to 0.
|
|
MinX, MinY, MaxX and MaxY are used to hold the boundary area that the ball is allowed to travel in. These points are based on the size of the boundary Object and are corrected to take into account the radius of the ball.
|
|
Batradius is used to hold the combined radius of the bat and the ball. This is calculated from bat._width / 2 + ball._width / 2. This radius is used when checking a collision condition. A collision has occurred when the two Objects are within Batradius of each other (ie. touching or overlapped).
|
|
The CalcNewDx() and CalcNewDy() functions can be entered via Add Script | Define function (...) (Leave all parameter fields blank). The functions are used to calculate the new x and y velocity of the ball after a collision with the bat. Functions are used here as they provide a way of grouping sections of code. This can be used to help keep each module small enough to be easily understood. For maximum readability, no function should extend beyond 1 page of display. Initially, the functions do not calculate the physics associated with the ball bouncing off the bat. The bat is assumed to be a 'sticky' bat, i.e. the ball simply adopts the current velocity of the bat.
|
The onEnterFrame() Event function is called on each Frame. It performs the following tasks:
|
· | updates current ball position based on current dx, dy values
|
· | checks for collision with boundary. Reverses dx, dy if collision is detected
|
· | checks for collision with bat. Updates dx, dy according to values returned from CalcNewDx() and CalcNewDy() functions.
|
7. | Test the game by selecting the 'Layout' tab and pressing the 'Play' button. The balls should be stationary. Click on the red ball (bat object) with the mouse. The bat will now follow the mouse pointer until you click the mouse again. Use the bat to hit the blue ball. The blue ball should continue to bounce around the court until it meets the bat again
|
8. | Press the 'Stop' button. Select the ball Sprite in the 'Layout' Panel and right click. Select Make Instance. If the new instance appears off the page, drag it back into the playing area. Press the 'Play' button. There are now two ball Objects that can be hit around the court. Note that the Sprite and the Instance behave independently of each other as each of their variables and associated properties (dx, dy, _X, _Y etc.) are local to the individual Object
|