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

2017/3/29

Game is stopping

salvo salvo

2017/3/29

#
Hey after getting to the next Level my game work perfectly the only problem is when the ObjectA get hit by an Bullet (ObjectB) the game stops and i need to rerun the game. Normally it should only add a score to the counter if you need more codes just ask thanks
salvo salvo

2017/3/29

#
import greenfoot.*;

/**
 * Write a description of class ProjektilGelb here.
 * 
 * @author  
 * @author 
 * @version 0.69
 * 
 */
public class ProjektilGelb extends Actor
{
 
       /**
         * diese Methode wird Abfragen ob ProjektilGelb getroffen hat -> 
         * hat es die Rakete getroffen wird der Score um 1 erhöht.
         * Das Projektil wird dabei verschwinden
         */
        
    public void act()//ProjektilGelb tut was auch immer in der "act" Methode drin steht
    {
        moveEast();//bewegt sich nach Osten (rechts)
 
        if (atWorldEdge())  
        {  
            getWorld().removeObject(this);  
        }
        else if (hitDetection()) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(this);   
            
            SpaceWelt SpaceWelt = (SpaceWelt)myWorld;
            
            Counter1 counter1 = SpaceWelt.getCounter1();
            counter1.addScore(); 
            
        }
    }
    /**
     * Abfrage ob das Projektil auf einen Gegenstand trifft
     */
    public boolean hitDetection()  
    {
        if (isTouching(Rocket2.class))
            return true;
        else
            return false;
    }     
    /**
     * Abfrage ob das Projektil am Ende der Welt ist
     */
    public boolean atWorldEdge()
    {  
        if(getX() == 100)  
            return true;  
        else
            return false;  
    }  
    /**
     * Methode, bei der das Projektil nach links fliegt
     */
    public void moveEast()//bewegung von ProjektilGelb wird deffiniert 
    {
        setLocation(getX()+2,getY());//bewegt sich nach rechts
    }
}

salvo salvo

2017/3/29

#
The next level would be SpaceWelt2 but i dont know how to add it so that the counter can add a score
Nosson1459 Nosson1459

2017/3/29

#
The same thing you did for the first level you can do for the second.
danpost danpost

2017/3/29

#
It looks like you changed the way you are going from level to level. You previously have it where the next level was in the same world and you removed some object and maybe changed the background and added some new objects. Now, it appears you are using a whole new World object for level 2. Because you have coded many of your Actor subclasses to work in only one type of world, this change has created some complications. For example, in your ProjektilGelb class code above, line 33 requires that the actor be in a specific type world -- a SpaceWelt world. Your current options are: * going back to having a single subclass of World; * using a bunch of 'if' statements to determine the type of world the actors are in by using the 'instanceof' method on the world returned by 'getWorld'; * adding a subclass of World that all your current subclasses of World can extend so that no matter what type of world the actor is in, it would be one of this super type; * cheat the system by making the Counter1 field and its getter method 'static' and having each level add the same counter into their respective worlds.
salvo salvo

2017/3/30

#
i would like to get the ethod static in the counter how do i do this ? this is my counter }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * Write a description of class Counter1 here.
 * 
 * @author Günter Amann 
 * @author Salvatore Lauricelli
 * @version 0.69
 */

 
public class Counter1 extends Actor
{
    int score = 0;
     
    public Counter1()
    {
        updateImage(); // set initial image
    }
 
    public void addScore()
    {
        score++; 
        updateImage(); // score changed, reset image
        if (score == 3) // check for level change
        {
            Greenfoot.setWorld(new SpaceWelt2());
        }
    }
     
    private void updateImage()
    {
        setImage(new GreenfootImage("Score: "+score, 24, Color.WHITE, Color.BLACK));
    }
}
salvo salvo

2017/3/30

#
else if (hitDetection()) 
        {
            World myWorld = getWorld();
            getWorld().removeObject(this);   
            
            SpaceWelt SpaceWelt = (SpaceWelt)myWorld;
            Counter1 counter1 = SpaceWelt.getCounter1(); 
            counter1.addScore(); 
            
            
        }
this is the part of the bullet which adds a Score to SpaceWelt (LVL1) maybe i cloud add something here so that the score will be added to SpaceWelt2(LVL2) ,too
danpost danpost

2017/3/30

#
After looking more closely into what you actual have, I find that there is one more option, which is probably the best one. Because your Counter1 class is written to specifically be a score counter and you seem to only have one score counter in any world, you can use the following for the 'hitDetection' code:
else if (hitDetection())
{
    ((Counter1)getWorld().getObjects(Counter1.class).get(0)).addScore();
    getWorld().removeObject(this);
}
which should work in any world. In the Counter1 class, you can adjust the level changing code to the following:
if (score == 3 && getWorld() instanceof SpaceWelt)
{
    SpaceWelt2 sw2 = new SpaceWelt2();
    ((Counter1)sw2.getObjects(Counter1.class).get(0)).score = 3;
    Greenfoot.setWorld(sw2);
}
The added condition will prevent SpaceWelt2 worlds from being created over and over again. The score counter in the new level is adjusted before setting the level active.
salvo salvo

2017/3/30

#
Ehm I have Have two counters for two bullets (two actors ) So this don't work either :) Sorry für the missing Information
salvo salvo

2017/3/30

#
Counter 1 and Counter 2 are identical
danpost danpost

2017/3/30

#
salvo wrote...
I have Have two counters for two bullets (two actors ) So this don't work either
But ... it will.
salvo wrote...
Counter 1 and Counter 2 are identical
Because they are two different types -- Counter1 and Counter2.
salvo salvo

2017/3/30

#
I tried it At it doesnt work The Game stops when the counter want to add a score
danpost danpost

2017/3/30

#
salvo wrote...
I tried it At it doesnt work The Game stops when the counter want to add a score
Show the code to your SpaceWelt class.
salvo salvo

2017/3/30

#
hey dan I insert may spacWelt2 in spaceWelt know everthing works thanks for your help
You need to login to post a reply.