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

2015/4/28

How to make a random flying object?

alwanammar alwanammar

2015/4/28

#
hello guys, ive got a problem with a random flying object. So, there's 2 actors(class) inside my scenario that'll fly. The first object flying from right to left and the second is the opposite. Then when the both of objects reachs the edge they'll turn and flying to the opposites direction. The problem is, i want to make a shooting game. When the bullet hit the actor, the actor should've been gone and then respawn at a random place. but they're moving upside down continuously at the edge of screen. Can you help me? this is inside Superclass called fly
int q = 0;
    private int Evil = 0;
    /**
     * Act - do whatever the Fly wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void flyy()
    {
        if(isAtEdge() ||getX()>750)
        {
            setLocation(getX(), 50+Greenfoot.getRandomNumber(320));
            turn(180);
            this.getImage().mirrorVertically();
            q++;
        }
    }
    public void right()
    {
        move(3/2);
    }
    public void left()
    {
        move(-3/2);
    }
    public void touch()
    {
        if(isTouching(Projectile.class))
         {
            MonkeyWorld MonkeyWorld = (MonkeyWorld) getWorld();
            MonkeyWorld.poin = MonkeyWorld.poin + 10;
            MonkeyWorld.changeScore(MonkeyWorld.poin);
            removeTouching(Evil2.class);
            setLocation(0, 50+Greenfoot.getRandomNumber(320));
         }
    }
    public void touch2()
    {
        if(isTouching(Projectile.class))
         {
            MonkeyWorld MonkeyWorld = (MonkeyWorld) getWorld();
            MonkeyWorld.poin = MonkeyWorld.poin + 10;
            MonkeyWorld.changeScore(MonkeyWorld.poin);
            removeTouching(Evil1.class);
            setLocation(800, 50+Greenfoot.getRandomNumber(320));
         }
    }
this is actor1
public void act() 
    {
        left();
        flyy();
        touch2();
    } 
and this is actor2
public void act() 
    {
        right();
        flyy();
        touch();
    }    
danpost danpost

2015/4/28

#
Line 48 will move a shot flyer to an x coordinate of 800 (or maybe 799, if the world is 800 pixels wide and bounded). Anyway, line 13 will then be continuously true (the x value will be greater than 750). The actor will turn around and move back and forth between 799 and 798. First, try changing the line 13 to this:
if (getX() == 0 || getX() == 799)
alwanammar alwanammar

2015/5/4

#
danpost wrote...
Line 48 will move a shot flyer to an x coordinate of 800 (or maybe 799, if the world is 800 pixels wide and bounded). Anyway, line 13 will then be continuously true (the x value will be greater than 750). The actor will turn around and move back and forth between 799 and 798. First, try changing the line 13 to this:
if (getX() == 0 || getX() == 799)
everything seems good for a few time, but its starting to messed up again after i tweaked it (i tweaked it because sometimes the flying object disappear(a bug)). So, if the flying object move from right to left then bounced back and get shot by the bullet it'll starts to go upside down at the right side. note: i made a two version of flying object. first from left to right. and the second is from right to left. the first flying object works just fine. the difference in code between those two is if the first used setlocation at 0 X axis, then the second's setlocation is at 800(or799) X axis this is the superclass
int q = 0;
    private int Evil = 0;
    /**
     * Act - do whatever the Fly wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
    }    
    public void flyy()
    {
        if (getX() == 0 || getX() == 799)
        {
            setLocation(getX(), 50+Greenfoot.getRandomNumber(320));
            turn(180);
            this.getImage().mirrorVertically();
            q++;
        }
    }
    public void flyback()
    {
        if (getX() < -5 || getX() > 805)
        {
            setLocation(800, 50+Greenfoot.getRandomNumber(320));
            flyy();
        }
    }
    public void right()
    {
        move(3/2);
    }
    public void left()
    {
        move(-3/2);
    }
    public void touch1()
    {
        if(isTouching(Banana.class))
         {
            MinionWorld MinionWorld = (MinionWorld) getWorld();
            MinionWorld.poin = MinionWorld.poin + 10;
            MinionWorld.changeScore(MinionWorld.poin);
            removeTouching(Evil1.class);
            setLocation(800, 50+Greenfoot.getRandomNumber(320));
            left();
         }
    }
and this is inside the subclass
    public void act() 
    {
        left();
        flyy();
        touch1();
        flyback();
    }  
please help n guide this noob :)
danpost danpost

2015/5/4

#
You said the flying object sometimes disappears (a bug). Do you mean the flying object is a bug? or the fact that it disappears is a bug? What classes are super to the fly class (name them up to the Actor class stating each using a phrases like 'fly extends Animal' -- 'Animal extends Actor').
alwanammar alwanammar

2015/5/9

#
could you please take a look in here? http://www.greenfoot.org/scenarios/13878 please check the MovingObject(superclass) and all Evil1,Evil2,Evil3,Evil4 class. whenever i shot Evil2 and Evil4 at the same time, one of them is just disappear. same with Evil1 and Evil3, but its occasionally. i tried to change the image (i set Evil1 and Evil3 into different image one is 1.png the other is 7.png). split Evil1 into dual actor (being Evil1 and Evil3), make axis location as a parameter. so does the Evil2/4 too. but it no uses and im kinda frustrated with this major bugs. and if you dont mind, please take a look at surprise class. i wanted it to get rid of surprise when the banana touch it. i used the same code to Evil classes when touching banana but it just doesnt work in surprise.class case. any idea?
danpost danpost

2015/5/9

#
With the Surprise object not being removed when touching a banana, you have the following in the MovingObject class:
public void notsurprised()
{
    if (isTouching(Banana.class)) removeTouching(Surprise.class);
}
The executable code:
removeTouching(Surprise.class);
is equivalent to this:
this.removeTouching(Surprise.class);
'this' is never included as a resultant object (an object is not considered touching or intersecting itself). You could, provided that only the Surprise class call the method, just use this:
getWorld().removeObject(this);
There is a general misplacement of methods in your project, however. You could move all the methods that each subclass calls into the class that calls them. This would be more appropriate, since they are specific to each particular type of object. Methods that would belong in the superclass (the MovingObject class) would be any that are used by multiple (if not all) subclasses. An example of a general method that you could use in the MovingObject class might be this:
public void removeSelf()
{
    if (getWorld() != null) getWorld().removeObject(this);
}
danpost danpost

2015/5/9

#
Noticed that your non-banana 'touch' methods are looking for the wrong type of objects (you probably copy/pasted the code from the 'touchBanana' method and did not adjust the code itself for the different types.
You need to login to post a reply.