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

2020/4/19

Actor won't spawn?

Cooleb Cooleb

2020/4/19

#
Heres the code, I'm not sure why it's not working when it works for all the other actors (the actor in question is the Bomb actor)
private int timerBomb = 0;
private int spawnTimerBomb;
int friendClicked = 0;
void friendlyClicked(){
    friendClicked += 1;
}
private void checkForSpawningBomb(){
    Bomb[] bomb = new Bomb[1];
    spawnTimerBomb = (spawnTimerBomb+1)%100;
    if (spawnTimerBomb == 0){
        for(int i = 0; i<bomb.length;i++){
            bomb[i] = new Bomb();
            addObject(bomb[i], Greenfoot.getRandomNumber(getWidth()), getHeight()-7);
            bomb[i].setRotation(270);
            bomb[i].turnTowards(Greenfoot.getRandomNumber(getWidth()), 0);
}
}
}
}
Also, could someone tell me how I could have this code remove another actor when it's clicked? That would be appreciated as well but this is the primary focus for me at the moment
danpost danpost

2020/4/19

#
Where is this checkForSpawningBomb method being called from?
Cooleb Cooleb

2020/4/20

#
danpost wrote...
Where is this checkForSpawningBomb method being called from?
the act method I believe
danpost danpost

2020/4/20

#
Cooleb wrote...
the act method I believe
Where is its (the act method's) code? It is not shown in what is given above.
Cooleb Cooleb

2020/4/20

#
danpost wrote...
Cooleb wrote...
the act method I believe
Where is its (the act method's) code? It is not shown in what is given above.
sorry i'm not so well versed in greenfoot. Should i just post the entire world code? I'm kind of confused
danpost danpost

2020/4/20

#
Cooleb wrote...
sorry i'm not so well versed in greenfoot. Should i just post the entire world code? I'm kind of confused
Sure. That would be fine.
Cooleb Cooleb

2020/4/21

#
danpost wrote...
Cooleb wrote...
sorry i'm not so well versed in greenfoot. Should i just post the entire world code? I'm kind of confused
Sure. That would be fine.
alright then
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    Target target;
    int multi;
    int i = 1;
    int curScore = 0;
 
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        
        super(700, 540, 1); 
        //Target target = new Target();
        //target.setRotation(Greenfoot.getRandomNumber(270));
        //addObject(target, Greenfoot.getRandomNumber(getWidth()), getHeight()-7); 
        //target.turnTowards(Greenfoot.getRandomNumber(getWidth()), 0);
        TimeEllapsed timeEllapsed = new TimeEllapsed();
        Cursor cursor = new Cursor();
        if(friendClicked != 1){
        addObject(cursor, getWidth()/2, getHeight()/2);
    }
        else{
           removeObject(cursor);
        }
        addObject(new Score(), 90, 90);
        
        addObject(timeEllapsed, 580, 65);
    }
  
    
    void clickedTarget(){
        curScore += 1;
    }
    
    int getCurrentScore() {
        return curScore;
    }
    
    private int timer = 0;
    public void act()
    {
        checkForSpawning();
        checkForSpawningDifEnemy();
    }
    //public void endGame(){
      //   if(targHit == 5){
      //      Greenfoot.stop();
      //  }
   // }
    private int spawnTimer;
    
    private void checkForSpawning() // call from act method
    {
        Target[] target = new Target[1];
        spawnTimer = (spawnTimer+1)%100;
        if (spawnTimer == 0) // at each timer reset
        {
            for(int i = 0; i<target.length;i++){
            {
                target[i] = new Target();
                addObject(target[i], Greenfoot.getRandomNumber(getWidth()), getHeight()-7);
                target[i].setRotation(Greenfoot.getRandomNumber(270));
                target[i].turnTowards(Greenfoot.getRandomNumber(getWidth()), 0);
            }
        } 
    }
}
  //  public void endGame(){


private int timer2 = 0;
private int spawnTimer2;
void clickedTargetDif(){
        curScore += 2;
    }
    private void checkForSpawningDifEnemy() // call from act method
    {
        TargetDif[] targetDif = new TargetDif[1];
        spawnTimer2 = (spawnTimer2+1)%340;
        if (spawnTimer2 == 0) // at each timer reset
        {
            for(int i = 0; i<targetDif.length;i++){
            {
                targetDif[i] = new TargetDif();
                addObject(targetDif[i], Greenfoot.getRandomNumber(getWidth()), getHeight()-7);
                targetDif[i].setRotation(Greenfoot.getRandomNumber(270));
                targetDif[i].turnTowards(Greenfoot.getRandomNumber(getWidth()), 0);
            }
        } 
    }
}

private int timerBomb = 0;
private int spawnTimerBomb;
int friendClicked = 0;
void friendlyClicked(){
    friendClicked += 1;
}
private void checkForSpawningBomb(){
    Bomb[] bomb = new Bomb[1];
    spawnTimerBomb = (spawnTimerBomb+1)%100;
    if (spawnTimerBomb == 0){
        for(int i = 0; i<bomb.length;i++){
            bomb[i] = new Bomb();
            addObject(bomb[i], Greenfoot.getRandomNumber(getWidth()), getHeight()-7);
            bomb[i].setRotation(270);
            bomb[i].turnTowards(Greenfoot.getRandomNumber(getWidth()), 0);
}
}
}
}


sorry if the problem is really obvious
danpost danpost

2020/4/21

#
Your act method, starting at line 52, calls 2 methods, neither of which is the checkForSpawningBomb method. Add that call to your act method.
Cooleb Cooleb

2020/4/21

#
danpost wrote...
Your act method, starting at line 52, calls 2 methods, neither of which is the checkForSpawningBomb method. Add that call to your act method.
Oh my lord I cant believe I didn't see that. Thanks for the fix and for all the work you've done for the community!
danpost danpost

2020/4/21

#
When your MyWorld object (or, your world) is created, the initial value of friendClicked is set to zero by line 107 (fields are created and set to their initial values before the constructor is executed). Since a constructor is only executed once per object created, the condition on line 31 will always be true and the else part of that if-else structure will never execute. Anything done after initial creation of an object cannot feasibly be coded in the constructor. Sometimes, some finalizing set-up is required once an actor is added to a world or when a world is activated by clicking the Run button provided by greenfoot. There are special methods to deal with those situations (addedToWorld(World) and started). Any codes performing non-initializing actions need to be addressed by (coded in or called from) an act method either in a World or Actor subclass). Only the currently active world and actor objects in that world will have their act methods executed while a scenario is in a running state.
You need to login to post a reply.