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

2014/1/8

Need help !! :(

TwoiBratan TwoiBratan

2014/1/8

#
Hello , How i let things go from the right site to the left ? Wie lasse ich dinge von der rechten Seite auf die linke von alleine rĂ¼ber bewegen ?
danpost danpost

2014/1/8

#
The 'getX' and 'getY' methods of the act class return the current coordinates of the actor. To move left, the x-coordinate must decrease. In the act method of the class of the actor you wish to have this behavior:
setLocation(getX()-1, getY());
The '1' can be changed to any other number or you can use an instance field to track the speed of the actor and use it there.
TwoiBratan TwoiBratan

2014/1/8

#
Thank you !! Now i have a problem ..
    public void act() 
    {
        keyisdown();
        touching();
        changeimage();

    }          

    public void keyisdown()
    {
        if(Greenfoot.isKeyDown("up"))
        {move(5);
            if(Greenfoot.isKeyDown("left"))
            {turn(-5);}
            if(Greenfoot.isKeyDown("right"))
            {turn(5);}
            if(Greenfoot.isKeyDown("down"))
            {move(-5);}

        } 
    }

    public void changeimage()
    {

        if(Greenfoot.isKeyDown("up"))
        {setImage("rocket.png");}
        else
        {setImage("rocket1.png");}

    }

    public void touching()
    {
        if(isTouching(Moon.class))
            Greenfoot.stop();
The rocket stop before he rly touch the moon :(
danpost danpost

2014/1/8

#
That probably has to do with the size of your images. There is probably some transparent area around one or both of your images that is making the area of possible intersection larger than you want. If the moon object has a visually round image (not including any transparency; and, of course, all images are, all in all, rectangular), then (because your rocket object is visually not a standard shape; not circular nor rectangular) this detection can be more easily accomplished like you want in the class of the moon object using the 'getObjectsInRange' method.
TwoiBratan TwoiBratan

2014/1/8

#
The rocket and the Moon have transparency :P What is that for a Method ? Sorry i am new . I'm doing this for a school project :)
danpost danpost

2014/1/8

#
I was just explaining WHY you were probably getting that behavior; not that you had to do anything with the transparency. My suggestion was to move the detection code to the class of the moon object and use the 'getObjectsInRange' method instead of 'isTouching'.
TwoiBratan TwoiBratan

2014/1/8

#
How i write it ? if(GetObjectsInRange(1200,Moon.class)) Greenfoot.stop();
danpost danpost

2014/1/8

#
If you look in the Actor class documentation, you will find that 'getObjectsInRange' returns a java.util.List object. The java.util.List documentation shows a method called 'isEmpty' which returns a boolean indicating just that, whether the list is empty or not. Your statement above has several flaws (1) the method name begins with a lowercase 'g' (2) '1200' would pretty much stop the scenario at the start; that value should be the radius of the visual image of the moon object (3) the method returns a List object, not a boolean value (4).
if (!getObjectsInRange(100, Moon.class).isEmpty()) Greenfoot.stop();
The exclamation point ( ' ! ' ) negates the value so that the scenario will stop when the List returned by the method is NOT empty.
TwoiBratan TwoiBratan

2014/1/8

#
It works !! I really thank you ! I have one last question to you ! Can i do this with the flying comets too ? They fly from right to left and when they touch the rocket it should stop the game . sorry for my english >.<
TwoiBratan TwoiBratan

2014/1/8

#
Have it already !! :)) Thank you very much :) ! But i dont understand that methode
   public void end()
    {
                if (getObjectsInRange(100, Rocket.class).isEmpty()) Greenfoot.stop();  
    }
can you explain it in a easy english :D?
danpost danpost

2014/1/8

#
(1) 'getObjectsInRange' returns a list of all Rocket object within 100 pixels of the center of the actor (the moon, in this case); see here; please note it returns a 'java.util.List' object. (2) 'isEmpty' returns a boolean value indicating whether the list object from (1) is empty or not; see here; (3) the condition in (2) determines whether the scenario should stop or not, using the 'stop' method of the Greenfoot class; see here.
You need to login to post a reply.