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

2017/4/1

Button Fire / Button shoot

1
2
3
iban iban

2017/4/2

#
the logic, if i press buttonshoot, then rocket will shoot :)
danpost danpost

2017/4/2

#
Okay. A best place to put the code is in the buttonshoot (tambak) class. Remove all previous codes that try to shoot and change the act method in the tambak class to:
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        addObject(new Bullet(), getX(), getY());
    }
}
iban iban

2017/4/2

#
this code i try. but position not in roket but the bullet position in buttonshoot. i want if i press the button shoot, then the bullet position in roket. this game popular shoot. have the button for shoot and navigation. please help me mr. :)
Super_Hippo Super_Hippo

2017/4/2

#
Adjust class names as needed:
//in Player class

private Actor shootButton = new Button();


protected void addedToWorld(World w)
{
    w.addObject(shootButton, x-coordinate, y-coordinate); //use x and y of the position where the button should be
}

public void act()
{
    if (Greenfoot.mousePressed(shootButton))
    {
        addObject(new Bullet(), getX(), getY());
    }
}
iban iban

2017/4/2

#
if i put this code in class roket, this not work. but it i'm change this to if (Greenfoot.mousePressed(this)), this work but i must press the rocket not button shoot :(
iban iban

2017/4/2

#
i want press the button shoot and rocket fire the bullet. Logic its same of the code. but not work :(
Super_Hippo Super_Hippo

2017/4/2

#
Do you only add the button from the rocket class and not from the world?
iban iban

2017/4/2

#
i add button shoot in world. addObject(new buttonshoot(), 200, 300); and no problem. i add one more object, this is roket object. i put the your code in class roket, but not work. i'am sory, please me :)
danpost danpost

2017/4/2

#
Try this in the bulletsoot class:
public void act()
{
    if (Greenfoot.mousePressed(this) && !getWorld().getObjects(Rocket.class).isEmpty())
    {
        Actor rocket = (Actor)getWorld().getObjects(Rocket.class).get(0);
        addObject(new Bullet(), rocket.getX(), rocket.getY());
    }
}
iban iban

2017/4/2

#
the project i post. you can help me for look that :)
Super_Hippo Super_Hippo

2017/4/2

#
Ok, so you control the rocket from your world class. Why are you not doing the same thing for the shooting?
iban iban

2017/4/2

#
uh i'm sory mr.super_hipo .. i want the all control have one button. i post my problem. can you see that? please me :) i'am very new in greenfoot
Super_Hippo Super_Hippo

2017/4/2

#
I did see it. That's why I asked you why the shooting button isn't a button like the moving buttons. Those buttons change a variable in the world which then controls the rocket based on those variables. (This is not the best way of doing it, but it is a way at least.) The shooting button could do a similar thing, change a variable in the world which lets the rocket shoot its bullet.
iban iban

2017/4/2

#
so i must make the method for the roket can shoot and that method i must make in world? ... oke i want try. thank mr :) but if you have time, you can help me solved that project, please me mr.:)
danpost danpost

2017/4/2

#
]In Latar class, change 'new tembak()' to 'new tembak(roket)'. Then have this for tembak class:
import greenfoot.*;

public class tembak extends Actor
{
    private roket roket;

    public tembak(roket roket)
    {
        this.roket = roket;
    }
    
    public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            getWorld().addObject(new peluru(), roket.getX(), roket.getY());
        }
    }
}
and for the roket class:
import greenfoot.*;

public class roket extends Actor
{
}
There are more replies on the next page.
1
2
3