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

2013/10/31

using portals to switch between world classes

skyhighh skyhighh

2013/10/31

#
hello i have a question, how can i switch between world classes with my hero class without lossing his stuff and without removing him from the world
danpost danpost

2013/10/31

#
Do not add any objects you need transferred to the new world in the new world. Just transfer them to the new world after creating it -- like this:
// from the hero class
World level2 = new Level2();
level2.addObject(this, /* x and y */);
// transfer other objects
Greenfoot.setWorld(level2);
skyhighh skyhighh

2013/10/31

#
hey tyvm dan post i had another question i have a counter class thats keeping the count for my health,coins and ammo. My probleem is how can i connect the class to my hero, cause now he only keeps te count of when i pick up stuff but when i use ammo or lose health i cant get it to react to that this is my counter class:
 private static final Color textColor = new Color(255, 180, 150);
    
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;
    private boolean coinFound = false;

    public Counter()
    {
        this("");
    }

    public Counter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);

        updateImage();
    }
    
    public void act() {
        if(value < target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
    }
    
   
    public void add(int score)
    {
        target += score;
    }

    public int getValue()
    {
        return value;
    }

    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
this is a part of one of my world classes:
public class Dungeon_1 extends World
{
    private Counter coinCounter; 
    private Counter ammoCounter;
    private Counter healthCounter;
   
    
    /**
     * Constructor for objects of class Dungeon1.
     * 
     */
    public Dungeon_1()
    {    
        super(800, 600, 1); 
        setBackground("dungeon_1 empty.png");
        createFloor();
        generateWorld();
        coinCounter = new Counter("Coins: ");
        addObject(coinCounter, 60, 580);
        ammoCounter = new Counter("Ammo: ");
        addObject(ammoCounter, 60, 560);
        healthCounter = new Counter("Health: ");
        addObject(healthCounter, 60, 540);
        //Greenfoot.playSound("Overworld_Theme.wav");
    }
    
     public Counter getCoinCounter()  
    {  
       return coinCounter;  
    }  
    
    public Counter getHealthCounter()
    {
        return healthCounter;
    }
    
    public Counter getAmmoCounter()
    {
        return ammoCounter;
    }
and these are the methodes from each of my pickup subclasses: Arrow:
  public void pickUp()
     {
         Actor hero = getOneIntersectingObject(Hero.class);
        if (pickingUp && hero == null)   // not touched anymore
        { 
            pickingUp = false;
        }
        else if (!pickingUp && hero != null)   // just being touched now
        {
            pickingUp = true;
            Greenfoot.playSound("LTTP_Item.wav");
           ((Dungeon_1)getWorld()).getAmmoCounter().add(10);
            getWorld().removeObject(this);
           
            
    }    
 
    }


HealthFlask: public void pickUp() { Actor hero = getOneIntersectingObject(Hero.class); if (pickingUp && hero == null) // not touched anymore { pickingUp = false; } else if (!pickingUp && hero != null) // just being touched now { pickingUp = true; Greenfoot.playSound("LTTP_Item.wav"); ((Dungeon_1)getWorld()).getHealthCounter().add(3); getWorld().removeObject(this); } } Coins:
  public void pickUp()
     {
         Actor hero = getOneIntersectingObject(Hero.class);
        if (pickingUp && hero == null)   // not touched anymore
        { 
            pickingUp = false;
        }
        else if (!pickingUp && hero != null)   // just being touched now
        {
            pickingUp = true;
            Greenfoot.playSound("LTTP_Rupee1.wav");
           ((Dungeon_1)getWorld()).getCoinCounter().add(5);
            Greenfoot.playSound("LTTP_Rupee2.wav");
            getWorld().removeObject(this);
        }    
    }
}
danpost danpost

2013/10/31

#
I believe your main problem stems from the fact that you put all the counter object fields in the world class. This will also cause problems later, when you change worlds to Dungeon_2. The ammo, health and coins are all states of your Hero object (the hero has so much ammo, so much health, and so many coins). These counter fields would be better placed in your Hero class. Also when you transfer the Hero to the next world, the counters are more easily accessible. Adding the counters into the world can be done from the Hero class using the 'addedToWorld' method. The method is called automatically by the system when your actor is added to the world and can be used to place the counters into the world.
skyhighh skyhighh

2013/10/31

#
tyvm i hope this works have deadline tommorow XD
skyhighh skyhighh

2013/11/1

#
hey danpost i have another question if you dont mind. I got everything working except the counter i cant get it to work from out my Playerclasse. You said i needed to use the addedtoworld methode.
skyhighh skyhighh

2013/11/1

#
this is my playerclasse it used to be hero classe. and i still have the same counter classe as before
    //Player inventory
    private int ammo;
    private int rupees;
    boolean bigKey;
    boolean smallKey;
        
    //Timers
    private int reloadTimer;
    
    //Arrays for player animations
    
    private String[] playerUp = { "LinkUp1", "LinkUp2", "LinkUp3", "LinkUp4", "LinkUp5", "LinkUp6", "LinkUp7" };
    private String[] playerDown = { "LinkDown1", "LinkDown2", "LinkDown3", "LinkDown4", "LinkDown5", "LinkDown6", "LinkDown7" };
    private String[] playerRight = { "LinkRight1", "LinkRight2", "LinkRight3", "LinkRight4", "LinkRight5", "LinkRight6", "LinkRight7" };    
    
    //Constructor for standard player
    public Player()
    {
        //Stats
        maxHealth = 100;
        currentHealth = maxHealth;
        speed = 3;
        
        //Inventory
        ammo = 10;
        rupees = 0;
        bigKey = false;
        smallKey = false;
        
        //Timers
        invincibilityTimer = 0;
        reloadTimer = 0;
        
        //Inventory usage
        direction = 0;
        
        //Animation
        frame = 0;
        delayCount = 0;
        frameDelay = 4;
        spriteLength = playerUp.length;
        shiftup = true;
    }
    
    //The player's actions
    public void act() 
    {
        //From Character
        animationCycle();
        wallCollision();
        blockCollision();
        invincibility();
        
        //From Player
        checkInput();
        reloadArrow();
        grabPickup();
        enterDoor();
        enterLockedDoor();
        
        //Hud
        //Hud hud = new Hud(getCurrentHealth(), getMaxHealth(), getAmmo(), getRupees());
    }
    
    public void takeDamage(int damage)
    {
        if(!invincibility())
        {
            currentHealth -= damage;
            if(currentHealth <= 0)
            {
                die();
            }
            else
            {
                Greenfoot.playSound("LTTP_Link_Hurt.wav");
                invincibilityTimer = 100;
            }
        }
    }
    
    //Check input from the user to move and perform actions, will show a non-walking sprite if no action is taken.
    public void checkInput()
    {
        if(Greenfoot.isKeyDown("s"))
        {
            swing();
        }
        else if(Greenfoot.isKeyDown("a") && ammo > 0)
        {
            shoot();
        }
        else if (Greenfoot.isKeyDown("up")) 
        {
            setLocation(getX(), getY()-speed);
            setImage(playerUp[frame]+".png");
            direction = 3;
        }
        else if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-speed, getY());
            setImage(playerRight[frame]+".png");
            GreenfootImage img = getImage();
            img.mirrorHorizontally();
            setImage(img);
            direction = 2;
        }
        else if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+speed, getY());
            setImage(playerRight[frame]+".png");
            direction = 0;
        }
        else if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY()+speed);
            setImage(playerDown[frame]+".png");
            direction = 1;
        }
        else
        {
            frame = 3;
            switch(direction)
            {
                case 0:     setImage(playerRight[frame]+".png");
                            break;
                case 1:     setImage(playerDown[frame]+".png");
                            break;
                case 2:     setImage(playerRight[frame]+".png");
                            GreenfootImage img = getImage();
                            img.mirrorHorizontally();
                            setImage(img);
                            break;
                default:    setImage(playerUp[frame]+".png");
                            break;
            }
        }    
    }

    //Let's the player swing his sword
    public void swing()
    {
        switch(direction)
        {
            case 0:     setImage("LinkSwingRight1.png");
                        break;
            case 1:     setImage("LinkSwingDown1.png");
                        break;
            case 2:     setImage("LinkSwingRight1.png");
                        GreenfootImage img = getImage();
                        img.mirrorHorizontally();
                        setImage(img);
                        break;
            default:    setImage("LinkSwingUp1.png");
                        break;
        }
        invincibilityTimer = 2;
    }
    
    //Makes the player shoot an arrow
    public void shoot()
    {
        if(reloadTimer== 0)
        {
            Arrow arrow = new Arrow(direction);
            Greenfoot.playSound("LTTP_Arrow_Shoot.wav");
            switch(direction)
            {
                case 0:     setImage("LinkShootRight1.png");
                            getWorld().addObject (arrow, getX()+10, getY());
                            break;
                case 1:     setImage("LinkShootDown1.png");
                            getWorld().addObject (arrow, getX(), getY()+10);
                            break;
                case 2:     setImage("LinkShootRight1.png");
                            GreenfootImage img = getImage();
                            img.mirrorHorizontally();
                            setImage(img);
                            getWorld().addObject (arrow, getX()-10, getY());
                            break;
                default:    setImage("LinkShootUp1.png");
                            getWorld().addObject (arrow, getX(), getY()-10);
                            break;
            }
            reloadTimer = 30;
            ammo--;
        }
    }
    
    //Player reloads his arrow
    public void reloadArrow()
    {
        if(reloadTimer > 0)
        {
            reloadTimer--;
        }
    }
    
    //Searches for pickups
    public void grabPickup()
    {
        Actor p = getOneIntersectingObject(Pickup.class);
        if(p != null)
        {
            Pickup pickup = (Pickup)p;
            switch(pickup.getItem())
            {
                case 0:     Greenfoot.playSound("LTTP_Item.wav");
                            addAmmo(5);
                            break;
                case 1:     Greenfoot.playSound("LTTP_RefillHealth.wav");
                            addHealth(25);
                            break;
                default:    Greenfoot.playSound("LTTP_Rupee1.wav");
                            addRupees(50);
                            break;
            }
            getWorld().removeObject(p);
        }
    }
    
    //Restores the player's health
    public void addHealth(int amount)
    {
        currentHealth += amount;
        if(currentHealth > maxHealth)
        {
            currentHealth = maxHealth;
        }
    }
    
    //Adds ammo to the player's inventory
    public void addAmmo(int amount)
    {
        ammo += amount;
    }
    
    //Adds rupees to the player's inventory
    public void addRupees(int amount)
    {
        rupees += amount;
    }
    
    //Returns the player's current health
    public int getCurrentHealth()
    {
        return currentHealth;
    }
    
    //Returns the player's total health
    public int getMaxHealth()
    {
        return maxHealth;
    }
    
    //Returns the player's ammo
    public int getAmmo()
    {
        return ammo;
    }    
    
    //Returns the player's rupees
    public int getRupees()
    {
        return rupees;
    }
    
    //When the player dies, an special sound and animation will show up
    public void die()
    {
        Greenfoot.playSound("LTTP_Link_Dying.wav");
        Dead_Link dl = new Dead_Link();
        getWorld().addObject (dl, getX(), getY());
        getWorld().removeObject(this);
    }
    
    public void enterDoor()
    {       
        Actor od = getOneIntersectingObject(OpenDoor.class);
        if(od != null)
        {
            OpenDoor opendoor = (OpenDoor)od;
            switch(opendoor.getId())
            {
                case 1:     World d2 = new Dungeon_2();
                            d2.addObject(this, 565, 235);
                            Greenfoot.setWorld(d2);
                            break;
                case 2:     World d3 = new Dungeon_3();
                            d3.addObject(this, 80, 240);
                            Greenfoot.setWorld(d3);
                            break;
                case 3:    World d4 = new Dungeon_1();
                            d4.addObject(this, 80, 240);
                            Greenfoot.setWorld(d4);
                            break;
                case 4:    World d5 = new Dungeon_1();
                            d5.addObject(this, 555, 240);
                            Greenfoot.setWorld(d5);
                            break;
                case 5:    World d6 = new Dungeon_4();
                            d6.addObject(this, 120, 95);
                            Greenfoot.setWorld(d6);
                            break;
                case 6:    World d7 = new Dungeon_3();
                            d7.addObject(this, 515, 380);
                            Greenfoot.setWorld(d7);
                            break;
                case 7:    World d8 = new Dungeon_1();
                            d8.addObject(this, 320, 85);
                            Greenfoot.setWorld(d8);
                            break;   
                case 8:    World d9 = new Dungeon_1();
                            d9.addObject(this, 320, 390);
                            Greenfoot.setWorld(d9);
                            break;             
                default:    World d1 = new Dungeon_1();
                            d1.addObject(this, 320, 390);
                            Greenfoot.setWorld(d1);
                            break;
            }
        }          
    }
    
    public void enterLockedDoor()
    {
         Actor ld = getOneIntersectingObject(LockedDoor.class);
        if(ld != null)
        {
            LockedDoor lockeddoor = (LockedDoor)ld;
            switch(lockeddoor.getId())
            {
                case 1:     World d10 = new Dungeon_5();
                            d10.addObject(this, 320, 390);
                            Greenfoot.setWorld(d10);
                            break;
                case 2:     World d11 = new Dungeon_6();
                            d11.addObject(this, 320, 390);
                            Greenfoot.setWorld(d11);
                            break;  
                default:    World d1 = new Dungeon_1();
                            d1.addObject(this, 320, 390);
                            Greenfoot.setWorld(d1);
                            break;            
    }
}
}
}
skyhighh skyhighh

2013/11/1

#
i even try to make ohter clase HUD but this didnt work for me either
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class Hud extends Actor
{   
    public Hud(int currentHealth, int maxHealth, int ammo, int rupees) 
    {
        GreenfootImage bg = getWorld().getBackground();
        bg.setColor(Color.WHITE);
        bg.drawString("Health: " + currentHealth + "/" + maxHealth + "\nAmmo: " + ammo + "\nRupees: "
        + rupees, 20, 420);
    }    
}
danpost danpost

2013/11/1

#
I was not suggesting to put int fields in your player class for the ammo, health, and coins. I was suggesting to put the Counter fields in the class (which, in themselves, will hold those int values). So, you create the counter objects in the Player constructor and add them into the world using the addedToWorld method.
skyhighh skyhighh

2013/11/1

#
oke tyvm one more question i just turn the whole code around and i only have like 2 or 3 hours till deadline do you know a way how i can show the variables of the player on the screen in text ??
danpost danpost

2013/11/1

#
As Counter (subclass of Actor) objects, you can just add them into the world using 'getWorld().addObject(counterName, x-location, y-location)'.
skyhighh skyhighh

2013/11/1

#
i made a methode inside the player classe and used this, but it doesn't show in my world
 public void addCounter()
    {
        coinCounter = new Counter("Rupee: ");
        getWorld().addObject(coinCounter, 48, 464);
        ammoCounter = new Counter("Ammo: ");
        getWorld().addObject(ammoCounter, 48, 448);
        healthCounter = new Counter("Health: ");
        getWorld().addObject(healthCounter, 48, 432);
    }
danpost danpost

2013/11/1

#
Again, you need to create the Counter objects in the player constructor and add them into the world from the addedToWorld method.
public Player()
{
    // code you already have
    coinCounter = new Counter("Rupee: ");
    ammoCounter = new Counter("Ammo: ");
    healthCounter = new Counter("Health: ");
}

public void addedToWorld(World world)
{
    world.addObject(coinCounter, 48, 464);
    world.addObject(ammoCounter, 48 448);
    world.addObject(healthCounter, 48, 432);
}
You need to login to post a reply.