hi,
i am doing a game in wich i need to use a ball.to make a effect of ball bouncing in the 2D i want to darw the ball in a parabolic path.so
can u give me the source code wich will return the points in a parabolic shape...
if(jumping) {
y -= speed;
jumpCounter++;
if(jumpCounter * speed > maxJump) {
jumpCounter = 0;
jumping = false;
falling = true;
}
} else if(falling) {
y += speed;
if(y <= groundx) {
falling = false;
}
}
DevelopmentTeam wrote:Request from @itskrishnaraj
- Code: Select all
if(jumping) {
y -= speed;
jumpCounter++;
if(jumpCounter * speed > maxJump) {
jumpCounter = 0;
jumping = false;
falling = true;
}
} else if(falling) {
y += speed;
if(y <= groundx) {
falling = false;
}
}
DevelopmentTeam wrote:I thing you have already answered your question. Make the path random. That should solve it.
DevelopmentTeam wrote:If you want to make the parabola random, then vary the xspeed and yspeed for every throw. This will give the difference. Some times the stone will fall near and sometimes far. Other you can try randomizing the xspeed alone.
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;
customfont midlet;
public RandomStoneCanvas(customfont m_Midlet)
{
midlet = m_Midlet;
rand = new Random();
stageWidth = getWidth();
stageHeight = getHeight();
newStone();
new Thread(this).start();
}
public void newStone()
{
xSpeed = 2 + rand.nextInt(20);
System.out.println("XSPEED- "+xSpeed);
ySpeed = - (10 + rand.nextInt(10));
System.out.println("YSPEED- "+ySpeed);
System.out.println("------------------------------------------");
xPosition = 50;
yPosition = 240;
/* xSpeed = 5 + rand.nextInt(20);
ySpeed = 0;
xPosition = 0;
yPosition = 100; */
}
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();
}
}
}
}Users browsing this forum: No registered users and 1 guest