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

2022/4/27

HELLO PLS HELP ME

Beebon Beebon

2022/4/27

#
CAN SOMEGIVE ME A CODE TO PIC ITEMS IN GREEN FOOT. PLS I HAVE BEEN WORKING ON THIS FOR AGES AND IT WILL NOT WORK.
Roshan123 Roshan123

2022/4/27

#
Hope it works all fine
//Global
boolean picked=false;

//Act method
if(isTouching(Gun.class) && Greenfoot.isKeyDown("p"))
  picked=true;
else if(Greenfoot.isKeyDown("d"))
  picked=false;

if(picked)
{
   Gun gun = (Gun)getWorld().getObjects(Gun.class).get(0);
   gun.setLocation(getX(), getY());
   gun.setRotation(getRotation());//optinal....not required according to what you asked for
}
Beebon Beebon

2022/4/27

#
it works but there is one problem only if i hold p it will get picked like how do i make it so it stay once i press p once. btw the gun after it shoots 3 bullets how do i make it dissapear. THANKS ALOT BTW. THANKS THANKS
Roshan123 Roshan123

2022/4/27

#
Beebon wrote...
the gun after it shoots 3 bullets how do i make it dissapear.
Use removeObject()/removeObjects() method for it. You can also use setTransparency() method but it will not remove the gun, instead it will make it transparent
Beebon wrote...
it works but there is one problem only if i hold p it will get picked like how do i make it so it stay once i press p once
Sorry but I don't get it. Some more clear details may help me out
Beebon Beebon

2022/4/27

#
so when the actor goes towards the gun and if u press "p" i want it to stay with character forever instead of holding down p. also when a gun shoots 5 bullets i need it disappear. thanks
danpost danpost

2022/4/27

#
Beebon wrote...
so when the actor goes towards the gun and if u press "p" i want it to stay with character forever instead of holding down p. also when a gun shoots 5 bullets i need it disappear. thanks
Add a Gun field to the class of the player:
private Gun gun = null;
To pick gun up:
if (Greenfoot.isKeyDown("p")) gun = (Gun)getOneIntersectingObject(Gun.class);
Moving with gun (after all player movement is completed):
if (gun != null) gun.setLocation(getX(), getY());
After shooting (shoot after moving gun):
if (gun.getWorld() == null) gun = null;
This will work if the gun removes itself when the bullets are depleted.
Beebon Beebon

2022/4/27

#
where do i add this
Beebon Beebon

2022/4/27

#
and how do i make it premently stay with the actor.
danpost danpost

2022/4/27

#
Beebon wrote...
where do i add this
All is in the class of the player. A gun does not shoot on its own -- the player pulls the trigger. However, you can give the gun the ability to fire a shot with a method:
public void shoot()
{
    getWorld().addObject(new Bullet(getRotation()), getX(), getY());
    shotsRemaining--;
    if (shotsRemaining == 0) getWorld().removeObject(this);
}
This will be called by the player with:
if (gun != null && Greenfoot.isKeyDown("space")) gun.shoot();
danpost danpost

2022/4/27

#
Beebon wrote...
and how do i make it premently stay with the actor.
danpost wrote...
Moving with gun (after all player movement is completed):
if (gun != null) gun.setLocation(getX(), getY());
Beebon Beebon

2022/4/28

#
thakns u
You need to login to post a reply.