|
|||||||||||||||||||||||||||
. . . . . |
Java For Kids Tutorial 8
| Intro |
Begin |
if/else |
Loops |
Arrays |
Graphics |
Animation |
Mouse |
Game |
Real |
Methods |
Class |
Class 2 |
Applet |
MouseClick |
Thread |
Button |
Java Mouse CaptureMethods
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Java Mouse MoveN.B. You will need the current(about 2 weeks after 16th march 2003) or later development version from judo.sourceforge.net which will be called JUDO 1.3. Mouse move capture is first set up by the use of the getMouseEvent() method. This method will return true if the mouse is moving.. Next we get the location of the X coordinate by using getMouseX() And finally find the Y coordinate by using getMouseY() Lets set up an algorithm for the first part... // initialise variables // start an infinite loop // check if mouse move returns true // get the location of the mouse // print the location of the mouse // restart infinite loop Lets do some code to flesh out the above algorithm
// initialise variables
int mx = 0;
int my = 0;
// start an infinite loop
while(true)
{
// check if mouse move returns true
if(getMouseEvent())
{
// get the location of the mouse
mx = getMouseX();
my = getMouseY();
// print the location of the mouse
printLine("x= "+ mx + "y = "+ my);
}
// restart infinite loop
}
Beautiful. Did you see all those numbers streaming down in the bottom textarea.. ? setColor(red); fillCircle(mx,my, 20);
What happened ?... Do you only want one red ball to be drawn on the screen ..? delay(0.10); clearDrawing(); Yep it worked.. but it needs one minor adjustment.. The mouse pointer is located at the top
right of the red ball and i want it in the center of the ball.. fillCircle(mx-10, my-10, 20); Here is the complete code ..
void main() {
// initialise variables
int mx = 0;
int my = 0;
// start an infinite loop
while(true)
{
// check if mouse move returns true
if(getMouseEvent())
{
// get the location of the mouse
mx = getMouseX();
my = getMouseY();
// draw circle at mouse posn
setColor(red);
fillCircle(mx-10, my-10, 20);
// set animation delay and clear screen
delay(0.10);
clearDrawing();
}
// restart infinite loop
}
}
Well how did that go.. there are a few problems.. The animation is jerky. And when the mouse is not moving, there is no ball painted.. As an exercise, see if you can solve those problems.. Java Mouse Click The method that will be used mainly for capturing mouse clicks will be
getMouseButton(int buttonNumber) . This method returns true if the corresponding number for a mouse
button is clicked.
Lets try it out !
void main() {
int mx = 0 ;
int my = 0 ;
// start infinite loop
while(true)
{
if(getMouseEvent())
{
// get mouse posns
mx = getMouseX();
my = getMouseY();
// check if left button clicked
if(getMouseButton(1))
{
printLine("Left mouse clicked at " + mx + " " + my);
}
// check if right button clicked
if(getMouseButton(3))
{
printLine("right mouse clicked at " + mx + " " + my);
}
}
}
}
}
Right! Now lets see if we can have the red ball be a defender in a game of
Aliens Invasion. We need to move the red ball back and forth along the bottom
whenver we move the mouse. // initialise x and y posn variables // start loop // check for mouse move or click // move red ball in tune with mouse // end loop Copy the algorithm into our judo text editor and fill in the code.. Try it out... What did you get ? Anything like this..?
void main() {
// initialise x and y posn variables
int mx = 0 ;
int my = 0 ;
// start loop
while(true) {
// check for mouse move or click
if(getMouseEvent())
{
mx = getMouseX();
my = getMouseY();
// move red ball in tune with mouse
setColor(red);
fillCircle(mx-10, getDrawingHeight()-20, 20);
delay(0.025);
clearDrawing();
}
// end loop
}
}
Now we want the defender to fire a bullet or missile.. And we want the bullet to fire
from the position that the defender is when the left mouse is clicked.
void main()
{
int mx = 0;
int my = 0;
int clickx = 0 ;
int clicky = getDrawingHeight() - 20;
int speed = 5 ; // 5 pixels per 0.025 seconds
boolean clicked = false;
// infinite loop
while(true){
if(getMouseEvent()) // check mouse event
{
mx = getMouseX();
my = getMouseY();
setColor(red);
fillCircle(mx-10, getDrawingHeight()-20, 20);
if(getMouseButton(1)) // left clik
{
clicked = true;
clickx = getMouseX();
}
}
if(clicked){ // set when left click
setColor(green);
fillCircle(clickx, clicky,10);
if(clicky > 0 )
{clicky = clicky - speed;}
else {clicked = false;
clicky=getDrawingHeight();}
delay(0.025);
clearDrawing();
}
}
}
We are getting somewhere but still lots of problems.. The red ball only gets erased when the button is pressed. The bullet only fires once. The clickx position is changing as we drag the mouse to left or right.. Try and have a go at solving these problems.. In the next lesson we will go on to designing our space invaders game and use the real Java compiler.. . |
|
|||||||||||||||||||||||||
|
| Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
|||||||||||||||||||||||||||