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

2017/5/23

Help Me Please :(

dadansi dadansi

2017/5/23

#
i want make the actor stop move if touch the grass
danpost danpost

2017/5/23

#
Collision detection has to be done in the class of one of the colliding actors. In order to move back off any hit grass, the actor needs to know where it was moved from. Therefore, the setting of the actor's location should also be done in its class. Make your world class act method simply this:
public void act()
{
    if ("atas".equals(mode)) imun.jalan_atas();
    if ("bawah".equals(mode)) imun.jalan_bawah();
    if ("kanan".equals(mode)) imun.jalan_kanan();
    if ("kiri".equals(mode)) imun.jalan_kiri();
}
Then, in each of the four called methods, use code similar to the following example:
public void jalan_atas()
{
    setLocation(getX(), getY()-3);
    if (isTouching(Rumput.class)) setLocation(getX(), getY()+3);
    waktu = ++waktu % 6;
    if (waktu) == 0)
    {
        frame = ++frame % 2;
        setImage(frame == 0 ? atas1 : atas2);
    }
}
Line 4 will prevent the actor from moving onto any grass objects. Finally, remove the 'nabrak_atas' method from the Imun class.
dadansi dadansi

2017/5/24

#
wow this very simple and work. thank you master, one more question, if i want make the button right, left or up like method isKeyDown, you know if button pressed, the actor move but if button not pressed, actor stop. how make that??
danpost danpost

2017/5/24

#
dadansi wrote...
if i want make the button right, left or up like method isKeyDown, you know if button pressed, the actor move but if button not pressed, actor stop. how make that??
That would not be as simple as just replacing the four conditions in the act method because you only would want one direction at a time (you were only able to click on one button at a time as originally coded, so this was not a issue with that). To allow only one direction at a time you need code as follows:
public void act()
{
    int dx = 0, dy = 0;
    if (Greenfoot.isKeyDown("left")) dx--;
    if (Greenfoot.isKeyDown("right")) dx++;
    if (Greenfoot.isKeyDown("up")) dy--;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (dx*dy  == 0 && dx+dy != 0) // this ensures one is zero and the other is not
    {
        if (dx == -1) imun.jalan_kiri();
        if (dx == 1) imun.jalan_kanan();
        if (dy == -1) imun.jalan_atas();
        if (dy == 1) imun.jalan_bawah();
    }
}
dadansi dadansi

2017/5/24

#
that code for game computer master. Greenfoot.isKeyDown not work to android. i use code mousePressed... how method mousePressed like isKeyDown. :) if i push the button left in game or i pressed the button left in game, that condition same with i push the button left in keyboard. and if i lost the button in game, that condition same with i lost the button left inkeyboard. this mousePressed master :)
dadansi dadansi

2017/5/24

#
one more, if i want make game for adroid with greenfoot, how big i must change pixel in greenfoot. 800x600?? or how big?
danpost danpost

2017/6/1

#
I will respond here since you asked for me specifically. Unfortunately, I am not at all familiar with the workings of android to help as far as screen sizing or what compiling program might work for you -- sorry.
You need to login to post a reply.