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

2022/4/28

super Mario powerup

1
2
Beebon Beebon

2022/4/28

#
so the idea is my actor-(Beep) or actor-(bebear) is going to collect a star, when the an actor does this how do u make him shoot fireballs which last for 30 seconds. I have been trying for a long time but still haven't got it yet. Thanks in advance
Roshan123 Roshan123

2022/4/28

#
If you remove the star after collecting it, write it inside your beep class
int x=1800;
if (getWorld().getObjects(Star.class).isEmpty())
x--;

if(x>=0 && x!=1800)
BeepShoot();
Beebon Beebon

2022/4/28

#
public void powerup() { Actor Star; Star = getOneObjectAtOffset(0 , 0 ,Star.class); if (Star != null) { World world; world = getWorld(); world.removeObject(Star); } } public adsasd() { getImage().scale(getImage().getWidth()/2,getImage().getHeight()/2); } public void act() { int x=1800; if (getWorld().getObjects(Star.class).isEmpty()) x--; if(x>=0 && x!=1800) BeepShoot(); like this?
Beebon Beebon

2022/4/28

#
i changed the sheep name to adsasd
Beebon Beebon

2022/4/28

#
btw how do i make it shoot very slowly
Beebon Beebon

2022/4/28

#
public void BeepShoot() { if(Greenfoot.isKeyDown("l") ) { getWorld().addObject(new fireball(getRotation()), getX(), getY()); } } public adsasd() { getImage().scale(getImage().getWidth()/2,getImage().getHeight()/2); } public void act() { int x=1800; if (getWorld().getObjects(Star.class).isEmpty()) x--; if(x>=0 && x!=1800) BeepShoot(); here the code for beep, but it is not working public class Star extends Actor { /** * Act - do whatever the Star wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (isTouching(adsasd.class)) { getWorld().removeObject(this); } } } code for the star
Roshan123 Roshan123

2022/4/28

#
Beebon wrote...
btw how do i make it shoot very slowly
That depends on the parameter you passed within move() method. Just decrease the value to make the bullet move slowly And about the code you provided, all seems fine according to me Note that if you are adding and collecting Star class simultaneously, then you have to reset the value of x to 1800 when x is equal to 0. By this you can use BeepShoot() method again and again
Beebon Beebon

2022/4/30

#
but even tho i dont collect the star it is shooting. how do i stop this
Roshan123 Roshan123

2022/4/30

#
Beebon wrote...
but even tho i dont collect the star it is shooting. how do i stop this
Oh, sorry, my bad. I didn't noticed it. Declare the variable x as a global variable rather than declaring it as local variable Here is how it looks like
int x=1800;
public void act{
if (getWorld().getObjects(Star.class).isEmpty())
x--; //to decrease the var x when star is not in world
 
if(x>=0 && x<1800)//To allow it to shoot when var x is greater than/equal to 0 second
BeepShoot();

if(x<0 && !getWorld().getObjects(Star.class).isEmpty())
x=1800;//to reset the var x to 1800, so that it can be used again to decrease it to 0
}
Btwn, please have a look on this. It helps users to identify which line is causing the problem
Beebon Beebon

2022/5/1

#
it still doesnt work. thanks for the code btw public void BeepShoot() { if(Greenfoot.isKeyDown("l")); getWorld().addObject(new fireball(getRotation()), getX(), getY()); } public adsasd() { getImage().scale(getImage().getWidth()/2,getImage().getHeight()/2); } int x=1800; public void act() { if (getWorld().getObjects(Star.class).isEmpty()) x--; //to decrease the var x when star is not in world if(x>=0 && x<1800)//To allow it to shoot when var x is greater than/equal to 0 second BeepShoot(); if(x<0 && !getWorld().getObjects(Star.class).isEmpty()) x=1800;//to reset the var x to 1800, so that it can be used again to decrease it to 0 BeepShoot();
Spock47 Spock47

2022/5/1

#
I think, it is always good to give the variables descriptive names. So, I cleaned it up a bit and added the logic for growing/shrinking of the image, too, and this code should do it (you can remove the touch-check from the Star class, too):
    private int remainingStarPower = -1;

    public void beepShoot()
    {
        if (Greenfoot.isKeyDown("l"))
        {
            getWorld().addObject(new fireball(getRotation()), getX(), getY());
        }
    }
    
    public void increaseImage()
    {
        getImage().scale(getImage().getWidth()*2,getImage().getHeight()*2);
    }
    
    public void decreaseImage()
    {
        getImage().scale(getImage().getWidth()/2,getImage().getHeight()/2);
    }
    
    public void act() 
    {        
        final Actor touchedStar = getOneIntersectingObject(Star.class);
        if (touchedStar != null)
        {
            getWorld().removeObject(touchedStar);
            if (remainingStarPower < 0)
            {
            	remainingStarPower = 1800;
            	increaseImage();
            }
            else
            {
                remainingStarPower += 1800;
            }
        }
        
        if (remainingStarPower > 0) {
            beepShoot();
        } else if (remainingStarPower == 0) {
            decreaseImage();
        } else {
            return; // to prevent remainingStarPower to go lower than -1
        }
        --remainingStarPower;
    }
Note: If the heroine selects a second star while the first is still active, the star stays active for ->additional<- 1800 cycles. The source code would be shorter, if the remaining star power would just be set to 1800 cycles when touching the star, but I like it better with putting 1800 additional to how much may have been left from the previous star. ;)
Roshan123 Roshan123

2022/5/1

#
@Spock47, just out of curiosity I wanna know why provided attempt is not working and most importantly, why are you using final keyword in line 23. Is it necessary. If yes, then why?
Beebon Beebon

2022/5/1

#
THANKS SO MUCH @Spock47 IT WORKS. THANKS THANKS THANKS THANKS
Beebon Beebon

2022/5/1

#
thanks to roshan123 for your time. :D
Spock47 Spock47

2022/5/1

#
Roshan123 wrote...
@Spock47, just out of curiosity I wanna know why provided attempt is not working and most importantly, why are you using final keyword in line 23. Is it necessary. If yes, then why?
The attempt was not fully working since "int x=1800;" is set independently from any star. So, the check
if(x>=0 && x<1800)//To allow it to shoot when var x is greater than/equal to 0 second
BeepShoot();
would allow shooting even before the star was collected. Also, if there wasn't a new star added in the world at the end of the star phase, it would immediately reset from -1 to 1800, right?
if(x<0 && !getWorld().getObjects(Star.class).isEmpty())
x=1800;//to reset the var x to 1800, so that it can be used again to decrease it to 0
That's why I modified your attempt so that the remainingStarPower/x variable only gets set to 1800 once the star has been collected, so that all the information needed is now in the remainingStarPower variable (basically "x>0 => you have star power"). With this, it was possible to remove the "no star available check", meaning the star power phase is now completely independent from the fact whether other (additional) stars exist in the game or not. In heart, it's still your solution; I only polished the edges a little bit. Regarding the final keyword: It is not necessary, it is only a "good practice" to use it where possible. It announces "I won't change this variable (reference) anymore", which makes source code easier to process: the compiler can (sometimes) optimize the resulting program with this information and a human that reads it knows the steps that now follow will all actually happen on the same object. But you can remove "final" and it stays functionally the same.
There are more replies on the next page.
1
2