Hi custom_made,
to get a "realistic" behaviour, you could try these steps:
* define some properties to store stone position (x, y) and speed (horizontal and vertical)
* when launching a new stone, initialize the stone position to some default value, and use a Random object to initialize the horizontal speed. You can assume initial vertical speed as zero, or initialize it as for the horizontal one, so the stone will not be initially thrown horizontally.
* after each step (tipically a repaint() request) you should modify both the stone speed and position:
** the vertical speed will change as for the gravity force effect (so you'll tipically add to it a constant value)
** the horizontal speed could be constant (if you assume no air resistance/wind/other forces) or you can gradually increase/decrease it at each step
** finally, you can calculate the new stone position by adding the single speed elements
This way, you should be able to have a quite random and realistic feeling for your stones.
Here's some basic sample code:
Code:
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class RandomStoneCanvas extends Canvas implements Runnable
{
int xPosition = 0;
int yPosition = 0;
int xSpeed = 0;
int ySpeed = 0;
int gravity = 1;
Random rand = null;
int stageWidth = 0;
int stageHeight = 0;
public RandomStoneCanvas()
{
rand = new Random();
stageWidth = getWidth();
stageHeight = getHeight();
newStone();
new Thread(this).start();
}
public void newStone()
{
xSpeed = 5 + rand.nextInt(20);
ySpeed = 0;
xPosition = 0;
yPosition = 0;
}
public void paint(Graphics g)
{
g.setColor(0xffffff);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0x000000);
g.fillRect(xPosition - 2, yPosition - 2, 4, 4);
}
void moveStone()
{
ySpeed += gravity;
xPosition += xSpeed;
yPosition += ySpeed;
if(xPosition > stageWidth || yPosition > stageHeight)
{
newStone();
}
}
public void run()
{
while(true)
{
try
{
repaint();
moveStone();
synchronized(this)
{
wait(100L);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
If you have any doubts about it, feel free to ask :)
Pit