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

2014/6/27

can't find MyWorld

1
2
Svek Svek

2014/6/27

#
hey im making a Space Invaders look a like. But the problem is that when i killed an enemy the score need to go up but Greenfoot can't find MyWorld.?! i think it is beacause it is not a real Greenfoot statement but i see codes all the time using MyWorld. If you know wye its not working please help me out
{
    /**
     * Act - do whatever the Bullit wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY() -10);
        if(getY() <= 20) 
        { 
            World SpaceWorld;
            SpaceWorld = getWorld();
            SpaceWorld.removeObject(this);

        }

        if ( getWorld() != null)
        {
            Enemy theEnemy;
            theEnemy = (Enemy) getOneIntersectingObject(Enemy.class);
            if (theEnemy != null)
            {
                World world;
                world = getWorld();
                //world.removeObject(theEnemy);          
                theEnemy.hitpoints--;
                world.removeObject(this);
                Counter score= myWorld.getScore();
                score.add(1);

            }
        }

     
    }
}
Svek Svek

2014/6/27

#
i mean myWorld not MyWorld
danpost danpost

2014/6/27

#
Some people use 'MyWorld' as the name of their subclass of World (their main world -- I guess). Your subclass of world is 'SpaceWorld' (I presume); so, you need to gain a reference to your SpaceWorld object to run the 'getScore' method. First, let me explain something. In lines 11 through 13 above, you are getting a reference to your world as a 'World' object so that you can call a World class method on it, which is fine (however, you can simply use:
getWorld().removeObject(this);
and avoid creating a local variable to hold the World object). Now, the big difference between calling 'removeObject' on the World and calling 'getScore' on the World is that the 'removeObject' method is a World class method and the 'getScore' method is a 'SpaceWorld' class method. You cannot simply say:
Counter score = getWorld().getScore();
The world returned from 'getWorld' must be specified as a SpaceWorld world. We do this by way of typecasting (casting the world as a SpaceWorld type of world). The following shows the basic casting of type:
SpaceWorld spaceWorld = (SpaceWorld)getWorld();
or broken down further:
SpaceWorld spaceWorld;
spaceWorld = (SpaceWorld) getWorld();
I could just as well have use 'world' instead of 'spaceWorld' as the name of the local variable; though, it would not have been as descriptive. If you removed the object from the world after getting the Counter object (moving line 27 down a line or two), you could use:
Counter score = ((SpaceWorld)getWorld()).getScore();
Svek Svek

2014/6/27

#
Thanks it helped alot
Svek Svek

2014/6/28

#
One more quistion. If i put the code:
Counter score = ((SpaceWorld)getWorld()).getScore(); 
in my game it doesn't recognize .getScore it sais: cannot find symbol - method getScore What to do??
danpost danpost

2014/6/28

#
Well, I presumed you had a 'public Counter getScore()' method in your SpaceWorld class (as per line 28 of your original post).
Svek Svek

2014/6/28

#
Yea i have it was a writing fold but the next problem popped up xD.
{
        setLocation(getX(), getY() -10);
        if(getY() <= 20) 
        { 
            World SpaceWorld;
            SpaceWorld = getWorld();
            SpaceWorld.removeObject(this);

        }

        if ( getWorld() != null)
        {
            Enemy theEnemy;
            theEnemy = (Enemy) getOneIntersectingObject(Enemy.class);
            if (theEnemy != null)
            {
                SpaceWorld spaceWorld;  
                spaceWorld = (SpaceWorld) getWorld();      
                theEnemy.hitpoints--;
                spaceWorld.removeObject(this);
               Counter score = ((SpaceWorld)getWorld()).getScore(); 
               score.add(1);
            }
        }
this is my code now. but if i start playing this warning pops up: java.lang.NullPointerException at Bullit.act(Bullit.java:36) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at Bullit.act(Bullit.java:36) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) And it sais something is wrong with this peace of code:
Counter score = ((SpaceWorld)getWorld()).getScore(); 
what shoud i do?
danpost danpost

2014/6/28

#
danpost wrote...
If you removed the object from the world after getting the Counter object (moving line 27 down a line or two), you could use:
Counter score = ((SpaceWorld)getWorld()).getScore();
This is what I had posted. Please follow along, here.
Svek Svek

2014/6/28

#
did it;
  setLocation(getX(), getY() -10);
        if(getY() <= 20) 
        { 
            World SpaceWorld;
            SpaceWorld = getWorld();
            SpaceWorld.removeObject(this);

        }

        if ( getWorld() != null)
        {
            Enemy theEnemy;
            theEnemy = (Enemy) getOneIntersectingObject(Enemy.class);
            if (theEnemy != null)
            {
                SpaceWorld spaceWorld;  
                spaceWorld = (SpaceWorld) getWorld();      
                theEnemy.hitpoints--;
                Counter score = ((SpaceWorld)getWorld()).getScore(); 
                score.add(1);
                spaceWorld.removeObject(this);
            }
        }
still gives the same error?
danpost danpost

2014/6/28

#
Please post your SpaceWorld class code.
Svek Svek

2014/6/28

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SpaceWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends World
{
    private Human thePlayer;
    private SpaceShip theEnemy;
    private Counter ScoreCounter;
    private GreenfootSound music = new GreenfootSound("Zelda Main Theme Song.mp3");
    
        
    public SpaceWorld()
    {    
        super(1180, 670, 1); 

        thePlayer = new Human();
        theEnemy = new SpaceShip();
        Spawn();
        HumanSpawn();
        music.playLoop();
        
    }

    public void HumanSpawn()
    {       
        addObject(thePlayer, getWidth()/2, getHeight()-40);  

    }

    public Human getPlayer()
    {
        return thePlayer;
    }

    public SpaceShip getEnemy()
    {
        return theEnemy;
    } 
    
    public Counter getScore()
    {
        return ScoreCounter;
    }

    public void Spawn()
    {
        for(int i=0; i<100; i += 100){
            // maak een nieuw ruimteschip en sla deze tijdelijk op
            SpaceShip theSpaceShip = new SpaceShip();

            // voeg het object toe en maak gebruik van zijn afmetingen voor de positionering

            addObject(theSpaceShip, 583 + i, 100);
        }

        for(int i=0; i<1000; i += 100){
            // maak een nieuw ruimteschip en sla deze tijdelijk op
            SpaceShip2 theSpaceShip2 = new SpaceShip2();

            // voeg het object toe en maak gebruik van zijn afmetingen voor de positionering

            addObject(theSpaceShip2, 130 + i,350);
        }

        for(int i=0; i<1000; i += 100){
            // maak een nieuw ruimteschip en sla deze tijdelijk op
            SpaceShip2 theSpaceShip2 = new SpaceShip2();

            // voeg het object toe en maak gebruik van zijn afmetingen voor de positionering

            addObject(theSpaceShip2, 130 + i,250);
        }

    }
    
   
}
Svek Svek

2014/6/28

#
if i did something dumb :p im new in this world
danpost danpost

2014/6/28

#
I do not see where you create a Counter object and set it to the 'ScoreCounter' field.
Svek Svek

2014/6/28

#
i don't really know what i need to fill in the public Counter
danpost danpost

2014/6/28

#
The 'public Counter getScore()' method is fine as it is. You just need to create a Counter object and set the 'ScoreCounter' field to refer to it:
ScoreCounter = new Counter(/** any arguments required */);
This should be done either when you declare the field, or in the constructor, or in a method that is called from the constructor. Being everything else in created in the constructor, I would put this code there as well. You may also want to add the object into the world.
There are more replies on the next page.
1
2