Tutorial:Alarm
From FANG
You should be able to follow these instructions without downloading or installing any software. You can use the FANG Engine Sandbox now without creating any account, or you can use the FANG Engine Playground.
Be sure read the Explanations of the Source Code in each of the tables below. Also if you are having trouble, try visiting the FAQ.
Under Construction
This page is currently being written. The instructions below should work for JavaWIDE. An Eclipse version is coming soon.
1. Make a New Page
In a new browser tab or window, open the FANG Engine Sandbox, FANG Engine Playground, or your local JavaWIDE site (your instructor will tell you about this if you have one). At the end of the address bar, you will see index.php/Main_Page.
To make a new page, you want to replace Main_Page with the title of the new page you want to create. Replace Main_Page with YourName/firstalarm/FirstAlarm where you replace YourName with your actual name. For example, if you name were Jane Doe, you would replace Main_Page with JaneDoe/firstalarm/FirstAlarm. The end of the address in the address bar should look like index.php/JaneDoe/firstalarm/FirstAlarm (with your actual name used).
Press enter to load the new page. You should see some text like:
There is currently no text in this page, you can search for this page title in other pages or edit this page.
Click on edit this page.
2. Make the Packages and Class
Click on the File:emptygame.png button. This generates the code you need to get started writing code. Click on the Show IDE button. This will show the Java Wiki Integrated Development Environment. This is where you can edit code more easily than in the simple text box. Click on the Hide IDE button. This will make the normal text box visible again. You can toggle the visibility of the IDE in this way.
3. Run the Game
Click on Save page. You should see an empty game appear.
4. JavaWIDE is easier to use than other IDEs
Using JavaWIDE is so easy to use, we don't need a step 4. Go to step 5.
5. Add a Sprite
To test alarms we need sprites. We're going to start by making a red triangle appear in the middle of the screen.
Click on edit at the top of the page. Add the code below which is not already there. Be sure to leave in the <java> tag at the beginning of the page and the </java> tag at the end of the page. These java tags indicate that what is between them is a Java program and not wiki text. You already have some of the code below from the previous instructions: new code is shown in bold.
After editing your code click on Save page
Note: the line numbers in this and each of the following tables do not necessarily match the line numbers in your program.
| A | Source code | Explanation of Source code |
|---|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package Jam.firstalarm;
import wiki.Wiki;
import fang.*;
import java.awt.*;
import java.awt.geom.*;
/**
* All about my game here.
* @author Jam Jenkins
*/
public class FirstAlarm extends GameLoop
{
private Sprite tri ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
}
private void makeSprites( )
{
tri = new PolygonSprite( 3 ) ;
tri.setScale( 0.1 ) ;
tri.setLocation( 0.5, 0.5 ) ;
tri.setColor( Color.RED ) ;
}
private void addSprites( )
{
canvas.addSprite( tri ) ;
}
}
|
Line 14: declares a Sprite instance variable that will be used to test the alarm. Lines 16-20: the startGame method is called when the Start button is pressed the first time. In this method, you need to make and add all the sprites. Note: for multiplayer games, the number of players is only available once the startGame method is called. Lines 22-28: helper method for constructing and placing sprites. The positions and sizes are relative to a screen which spans (0, 0) to (1, 1). Lines 30-33: helper method for adding sprites to the canvas. Simply making a sprite does not make it visible. Sprites are only visible once they are added to the canvas within the bounds of the canvas and sized properly. |
6. Test your changes
Always test your changes before making more modifications. Run the game. Nothing should change and nothing should be broken. If the game does not work, reread the instructions above to see what went wrong. A red triangle should now display on the screen.
7. Make an alarm
Alarms are a method of scheduling events to occur at times relative to when an alarm is created or in absolute time from when the program starts. We will begin by creating an alarm to fire after 1 second. Add the code snippets in bold to your existing code.
| B | Source code | Explanation of Source code |
|---|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package Jam.firstalarm;
import wiki.Wiki;
import fang.*;
import java.awt.*;
import java.awt.geom.*;
/**
* All about my game here.
* @author Jam Jenkins
*/
public class FirstAlarm extends GameLoop
{
private Sprite tri ;
public void startGame( )
{
makeSprites( ) ;
addSprites( ) ;
setupAlarm( 1 ) ;
}
private void makeSprites( )
{
tri = new PolygonSprite( 3 ) ;
tri.setScale( 0.1 ) ;
tri.setLocation( 0.5, 0.5 ) ;
tri.setColor( Color.RED ) ;
}
private void addSprites( )
{
canvas.addSprite( tri ) ;
}
private void setupAlarm( double time )
{
scheduleRelative(new MyAlarm( ), time ) ;
}
private class MyAlarm implements Alarm
{
public void alarm( )
{
tri.setVisible( false ) ;
}
}
}
|
Line 20: Makes the call to setupAlarm with an argument of 1 (corresponds to seconds, see below explanation of setupAlarm). Lines 36-39: setupAlarm takes one double argument that corresponds to time. This argument is supplied to the scheduleRelative method (part of FANG). There are two ways to schedule timers; relative and absolute. scheduleRelative makes the alarm fire the specified number of seconds after the alarm is created. scheduleAbsolute makes the alarm fire the specified number of seconds after the START of the program. The first argument supplied to these functions is a new Alarm object of the type you created--in this example MyAlarm. The second is the time argument for the number of seconds as discussed above. Lines 41-47: The creation of the class needed to create an Alarm. The Alarm class itself is an interface, meaning you cannot create objects of this type, so you must write a class of your own that implements Alarm. This class need only contain the public void alarm( ) method. Inside of alarm( ) you should place the code (or function call to code) that you want to execute when the alarm fires. In this case, we set the triangle to no longer be visible. |
8. Test your changes
Always test your changes before making more modifications. Run the game. A red triangle should appear and one second after you click start it should disappear. If the game does not work, reread the instructions above to see what went wrong.
9. Make the alarm repeat
Now we are going to set the triangle to "blink" off and on rather than just turn off. This will only require one edit and an additional line of code to your alarm( ) method within MyAlarm.
| C | Source code | Explanation of Source code |
|---|---|---|
1 2 3 4 5 6 7 8 |
private class MyAlarm implements Alarm
{
public void alarm( )
{
tri.setVisible( !tri.isVisible( ) ) ;
scheduleRelative( this, 1 ) ;
}
}
|
Line 5: Here the argument supplied to setVisible is changed. Instead of just saying false to make the triangle vanish, we now want it to disappear and reappear. To do this, we first get the triangle's current visibility by calling tri.isVisible( ), which returns true if it is visible and false otherwise. The '!' is an operator that reverses the value of a boolean. So, if isVisible returns true, the ! changes it to false and vice versa. This flipped value is then passed into setVisible. Line 6: This line schedules another alarm to happen in 1 second. The 'this' keyword is used to refer to the current object within the class executing the code. Because this code is inside the MyAlarm class, this refers to the current MyAlarm object. |
10. Test your changes
Always test your changes before making more modifications. Run the game. The red triangle should now switch between visible and invisible every second. If the game does not work, reread the instructions above to see what went wrong.
11. Comment the code and make your own modifications
The alarm works. Congratulations! Comment the code you've written/copied. Documenting your code is essential if you are to share it or collaborate with others. Now that you have a working version, experiment with it. Make modifications and enhancements.
- This page was last modified on 20 January 2008, at 16:53.
- This page has been accessed 2,404 times.
- Privacy policy
- About FANG
- Disclaimers
- Powered by MediaWiki!











