This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kaiwalya
Kaiwalya wrote ...

2013/7/30

How Can I make a object shake?

Kaiwalya Kaiwalya

2013/7/30

#
I am making an game in which i have to show that the plane is in an turbulence , so to show it I have to make it vibrate or shake.
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
I'm not a pro, but I think you could do something like this:
public class Plane extends Actor
{
int rn;
int crot;
public void act()
{
if(getOneIntersectingObject(Turbulance.class)!=null)
{
rn=Greenfoot.getRandomNumber(3)-2)//returns numbers -1, 0 and 1
crot=getRotation();//current angle
setRotation(getRotation()+rn);//rotate
if(crot>355&&>5) //if rotated too much, turn back
setRotation(crot+1);
if(crot<355&&<5) //if rotated too much, turn back
setRotation(crot-1);
}
else if(getOneIntersectingObject(Turbulance.class)==null) //when not in turbulance
{ //turn back to original angle
if(crot<0)                                                                       
setRotation(crot-1);    
if(crot>0)
setRotation(crot+1);
}
}
Hope I helped
Kaiwalya Kaiwalya

2013/7/30

#
In the line 09 its showing this error: rn=Greenfoot.getRandomNumber(3)-2 | ) ';' expected at this place ' | '.
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
Delete the ' | '
Kaiwalya Kaiwalya

2013/7/30

#
Thats just nothing ,I have used it to show u the place where it is asking for the ' ; '.
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
Oh sorry, it should look like this: rn=Greenfoot.getRandomNumber(3)-2;
danpost danpost

2013/7/30

#
There are a few problems with the code that 8bitcarrotjuice gave. (1) line 9 returns -2, -1 or 0 as written (2) as noted already, line 9 needs a semi-colon at the end of the statement (3) line 12 and 14 have improper conditionals (a) cannot use '>5' or '<5' as conditions (should be 'crot>5" and crot<5') (b) logically the conditions are not correct (4) the instance fields 'crot' and 'rn' should be local to the method (their values are not passed from one act cycle to the next, but reset each time) More to the point, the class should look like this:
import greenfoot.*;

public class Plane extends Actor
{
    public void act()
    {
        int rot = getRotation();
        if (getOneIntersectingObject(Turbulance.class) != null)
        {
            rot += Greenfoot.getRandomNumber(3)-1;
            if (rot>=180 && rot < 355) rot = 355;
            if (rot <=180 && rot > 5) rot = 5;
        }
        else
        {
            if (rot > 180) rot++;
            if (rot < 180 && rot != 0) rot--;
        }
        setRotation(rot);
    }
}
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
Thanks Dan, as I said I'm not that good at coding, but prior to your post I updated my code a bit; I wasn't that far off(!)
Kaiwalya Kaiwalya

2013/7/30

#
Is a Turbulance class is required for this codes????? Because I have not made one.
8bitcarrotjuice 8bitcarrotjuice

2013/7/30

#
What do you have so far?
danpost danpost

2013/7/30

#
No. You could use an int field to track windspeed and have it vary randomly. Then, when the difference between the speeds of the plane and wind are greater than a certain amount, create the shake; and if the difference is less than a certain amount, create loss of lift (if you want). If doing the lift thing, you will need an instance int field for it, too.
You need to login to post a reply.