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

2019/8/24

timer on spawning things

1
2
joejoe joejoe

2019/8/24

#
hey i need you help again how can i spawn an actor for like every one minute or something
danpost danpost

2019/8/24

#
joejoe wrote...
hey i need you help again how can i spawn an actor for like every one minute or something
In World subclass (MyWorld -- maybe):
private int spawnTimer;

public void act()
{
    spawnTimer = ++spawnTimer%60; // 60 is "time" between spawns (adjust as needed)
    if (spawnTimer == 0)
    {
        // add code to spawn actor here
    }
}
joejoe joejoe

2019/8/24

#
i already pasted it and looked like this (at the world subclass)
private int spawnTimer;
 
    private void checkForSpawning() // call from act method
   {
    spawnTimer = (spawnTimer+1)%600; // repeat every 10 seconds (about)
    if (spawnTimer == 0) // at each timer reset
    {
        addObject(new zombieSpawner());
    }
   } 
but it says"method addObject in class greenfoot.World cannot be applied to given types:" what should i do
danpost danpost

2019/8/24

#
joejoe wrote...
i already pasted it and looked like this (at the world subclass) << Code Omitted >> but it says"method addObject in class greenfoot.World cannot be applied to given types:" what should i do
The addObject method requires 3 arguments -- the actor and the x and y location coordinates where the actor is to be placed.
joejoe joejoe

2019/8/24

#
i already finished it but i want to make the "world" spawn a specific amount of the "zombiespawner" but i dont know where to put it
danpost danpost

2019/8/24

#
joejoe wrote...
i want to make the "world" spawn a specific amount of the "zombiespawner" but i dont know where to put it
"a specific amount" in total or at any one time? Either way, a simple if statement should do the trick (maybe with an extra int field).
joejoe joejoe

2019/8/24

#
i dont know what if statement that is or an extra int field bx i just got started in greenfoot
joejoe joejoe

2019/8/24

#
i mean can it be like a parameter or something?
danpost danpost

2019/8/24

#
joejoe wrote...
i dont know what if statement that is or an extra int field bx i just got started in greenfoot ... i mean can it be like a parameter or something?
To restrict something, you need an if condition. Maybe like:
if (getObjects(zombieSpawner.class).size < 5) addObject(...
joejoe joejoe

2019/8/25

#
i already got it thanks my next problem is like how do you apply this timer on a gun in my game, i have like a "super bullet" where it will go through any zombie, but i dont want it to get spammed many times how am i gonna get that interval in shooting that super bullet the code for the super bullet:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class superBullet extends Actor
{
    public int speed = 10;
    
    public superBullet(int rotation)
    {
        setRotation(rotation);
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/30;
        int myNewWidth = (int)myImage.getWidth()/50;
        myImage.scale(myNewWidth, myNewHeight);
        
    }
    
    public superBullet()
    {
        GreenfootImage myImage = getImage();
        int myNewHeight = (int)myImage.getHeight()/10;
        int myNewWidth = (int)myImage.getWidth()/10;
        myImage.scale(myNewWidth, myNewHeight);

    }
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(speed);
        remove();
        
    }    
    public void remove()
    {
     
        if(getX() >= getWorld().getWidth() -1)
        getWorld().removeObject(this);
        else if (getX() <1)
        getWorld().removeObject(this);
        else if(getY() >= getWorld().getHeight() -1)
        getWorld().removeObject(this);
        else if (getY() <1)
        getWorld().removeObject(this);   
    }
}
and the code for the shooter or the player (only in the shooting part):
public void shoot()
    {
        if(Greenfoot.getMouseInfo() != null)
        {
            if (!spaceDown && (Greenfoot.getMouseInfo().getButton() == 1))
        {
         spaceDown = true;
         getWorld().addObject(new Bullet(playerRotation), getX() , getY());
        }
         if (spaceDown && !(Greenfoot.getMouseInfo().getButton() == 1))
        { 
         spaceDown = false;
         }
            
            
        }
        
        
        
    }
thanks (in advance)
danpost danpost

2019/8/25

#
The MouseInfo objects only give you a button value when there is a change in that button. It does not continuously return a value while the button is down. You should therefore be using Greenfoott.mousePressed along with Greenfoot.getMouseInfo.getButton for button going down and Greenfoot.mouseClicked for button going up. Why you use a field name like spaceDown is beyond me. Would not btn1Down be more appropriate?
joejoe joejoe

2019/8/25

#
I am sorry if i used wrong terms or wrong field names bc i just got started in greenfoot. But my question is that where shall Greenfoot.mouse pressed be placed in the code?
joejoe joejoe

2019/8/25

#
Is it the same as greenfoot.iskeydown?
danpost danpost

2019/8/25

#
joejoe wrote...
I am sorry if i used wrong terms or wrong field names bc i just got started in greenfoot. But my question is that where shall Greenfoot.mouse pressed be placed in the code? ... Is it the same as greenfoot.iskeydown?
Definitely NOT the same. isKeyDown is a keyboard related while mousePressed is mouse related. They are both used for testing of a state, however, and are normally used as part (or all) of an if condition:
//
if (Greenfoot.mousePressed(null) && Greenfoot.getMouseInfo().getButton() == 1)
joejoe joejoe

2019/8/25

#
ok i changed it and it works much better thank you about that correction but lets go back to my main question where to add a "super bullet" but i want to add interval (let's say like 8 secs) so it will not be spamable
There are more replies on the next page.
1
2