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

2017/5/25

Phantom Object

1
2
DuckGod DuckGod

2017/5/25

#
So i was messing around and then i started to notice that things that i didn't put there are showing up there I don't have code that would add it in except in the world and i make sure to remove everything before going on to the next case so here's the code if anyone needs it later on From the World class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class TutorialLvl here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class WorldLoader extends World
{
    //private Egg egg = new Egg();
    static GreenfootImage background;
    static GreenfootImage bgmask;//bg means background
    GreenfootImage image;
    static int swidth=700;//S means Super
    static int sheight=500;//S means Super

    static GreenfootImage codes = new GreenfootImage("codes.gif");// The colors of grounds

    static Color groundColor;
    static Color AIColor;//AI means Artifical Intenigents
    static Color spikeColor;
    static Color haxColor;
    
    static Color secondGroundColor;

    static int currentLevel = 0;
    static int maxLevel = 99;//total amount of levels

    static public int playerx, playery;//for enemies

    static public int startx, starty;//the starting place of the player
    
    /**
     * Constructor for objects of class TutorialLvl.
     * 
     */
    public WorldLoader()
    {    
        super(swidth, sheight, 1); 
        setColors();// this sets the colors for all of the other actors
        restartGame();
        setPaintOrder(Egg.class,Door.class);
    }

    public void act()
    {
        lastlevel();
        otherStuffThatHappen();
    }

    private void lastlevel()
    {
        if (currentLevel == 10
        && playerx > 300
        && playery < 100
        )
        {
            changeLevel();
        }
    }

    private void setColors()
    {
        groundColor = codes.getColorAt(1,1);//black
        AIColor=codes.getColorAt(22,1);//red
        spikeColor = codes.getColorAt(57,1);//yellow
        haxColor = codes.getColorAt(40,1);//green
        
        secondGroundColor = codes.getColorAt(68,1);//cyan
    }

    public boolean isGround(Color mg)
    {
        if (groundColor.equals(mg)) 
        {return true;}
        return false;
    }

    public boolean isGround(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (groundColor.equals(ez)) 
        {return true;}
        return false;
    }
    
    public boolean isSecondGround(Color mg)
    {
        if(secondGroundColor.equals(mg))
        {return true;}
        return false;
    }
    
    public boolean isSecondGround(int x, int y)
    {
        if(x>swidth-1||x<0||y>sheight-1||y<0){return false;}
        
        Color ez=bgmask.getColorAt(x,y);
        if(secondGroundColor.equals(ez))
        {return true;}
        return false;
    }

    public boolean isPatrol(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (AIColor.equals(ez)) 
        {return true;}
        return false;
    }

    public boolean isInstaDeath(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (haxColor.equals(ez)) 
        {return true;}
        return false;
    }

    public boolean isSpike(int x, int y)
    {
        if (x > swidth-1 || x < 0 || y > sheight-1 || y < 0) {return false;}

        Color ez = bgmask.getColorAt(x,y);
        if (spikeColor.equals(ez)) 
        {return true;}
        return false;
    }

    static  public void paintBg(int x, int y)
    {
        background.setColorAt(x,y, Color.RED);
        background.setColor(Color.RED);
        background.fillRect(x,y, 10,10);
    }

    public void setPlayerCoords(int px, int py)
    {
        playerx=px;
        playery=py;
    }

    public void cleanUp()
    {
        this.removeObjects(this.getObjects(Egg.class));
        this.removeObjects(this.getObjects(Door.class));
        this.removeObjects(this.getObjects(Fall.class));
        this.removeObjects(this.getObjects(Consumables.class));
        this.removeObjects(this.getObjects(OtherConsumables.class));
        this.removeObjects(this.getObjects(ChoseStory.class));
        this.removeObjects(this.getObjects(BadGuys.class));
        this.removeObjects(this.getObjects(MonsterSpawner.class));
    }

    public void changeLevel()
    {
        currentLevel++;
        if(currentLevel > maxLevel)
        {
            currentLevel=0;
        }
        setLevel(currentLevel);
    }

    public void restartLevel()
    {
        setLevel(currentLevel);
    }

    public void restartGame()
    {
        currentLevel=0;
        setLevel(currentLevel);
    }

    public void setLevel(int i)
    {
        if (i <0 || i > maxLevel) {return;}
        cleanUp();
        currentLevel=i;
        switch(i)
        {
            case 0: testLvl(); break;
            case 1: testLvl2(); break;
            case 2: EzLvl(); break;
            case 3: RiskyMove(); break;
            case 4: TestLvlChange(); break;
            case 5: RiskTake(); break;
            case 6: SecondGroundTest(); break;
            case 7: Smurt(); break;
            case 8: Time(); break;
            
            //Play as the good guy
            case 9: IntroDuction(); break;
            case 10:JgAreaLvl1To2(); break;
            case 11://reserved
            case 12://reserved
            case 13:JgAreaLvl2To5();break;
            case 14://reserved
            case 15://reserved
            
            //Play as the bad guy         
            case 16:BadIntroDuction(); break;
            case 17:
            case 18:
            case 19:
            case 20:
            case 21:
            case 22:
            
            //Die Forever
            case 23: LOL(); break;
        }
    }
    
    public void otherStuffThatHappen()
    {
        if(Egg.foodType==1)
        {
            background=new GreenfootImage("ChangeBackground2.png");
            setBackground(background);
            bgmask=new GreenfootImage("TestMaskChange2.png");
            Egg.foodType=0;
        }
        
        if(Egg.foodType==2)
        {
            background=new GreenfootImage("OrNahBackground.png");
            setBackground(background);
            bgmask=new GreenfootImage("OrNahMask.png");
            Egg.foodType=0;
        }
    }

    public void testLvl()
    {
        background= new GreenfootImage("BackGroundTest.png");
        setBackground(background);
        bgmask = new GreenfootImage("MaskTest.png");
        startx= 130;
        starty=10;
        addObject(new Egg(), startx,starty);
        //addObject(new Door(false,0), 100,445);
        addObject(new ChoseStory(9,1),100,445);
    }

    public void testLvl2()
    {
        background=new GreenfootImage("SpikeTestBackground.png");
        setBackground(background);
        bgmask= new GreenfootImage("SpikeTestMask.png");
        startx=0;
        starty=0;
        addObject(new Egg(), startx,starty);
        addObject(new Door(false,1), 600,100);
    }
    
    public void EzLvl()
    {
        background=new GreenfootImage("EzLvlBackGround.png");
        setBackground(background);
        bgmask=new GreenfootImage("EzLvlMask.png");
        startx=680;
        starty=20;
        addObject(new Egg(), startx, starty);
        addObject(new Door(false,2), 540,200);
    }
    
    public void RiskyMove()
    {
        background=new GreenfootImage("LavaBackGround.png");
        setBackground(background);
        bgmask=new GreenfootImage("LavaMask.png");
        startx=120;
        starty=320;
        addObject(new Egg(),startx,starty);
        addObject(new Door(false,3),130,130);
    }
    
    public void TestLvlChange()
    {
        background= new GreenfootImage("ChangeBackground.png");
        setBackground(background);
        bgmask=new GreenfootImage("TestMaskChange.png");
        startx=20;
        starty=335;
        addObject(new Egg(),startx,starty);
        addObject(new Consumables(0),20,120);
        addObject(new Door(false,3),650,330);
    }
    
    public void RiskTake()
    {
        background=new GreenfootImage("RiskItBackground.png");
        setBackground(background);
        bgmask=new GreenfootImage("RiskItMask.png");
        startx=25;
        starty=300;
        addObject(new Egg(),startx,starty);
        addObject(new Consumables(1),500,10);
        addObject(new Door(false,1),650,330);
        for(int i=0;i<3;i++)
        {
            addObject(new OtherConsumables(0),490,90+90*i);
        }
    }
    
    public void SecondGroundTest()
    {
        background=new GreenfootImage("2edGroundTestBackground.png");
        setBackground(background);
        bgmask=new GreenfootImage("2edGroundTestMask.png");
        startx=50;
        starty=390;
        addObject(new Egg(),startx,starty);
        addObject(new Door(false,2),660,150);
    }
    
    public void Smurt()
    {
        background=new GreenfootImage("SmartBackground.png");
        setBackground(background);
        bgmask=new GreenfootImage("SmartMask.png");
        startx=10;
        starty=240;
        addObject(new Egg(),startx,starty);
        addObject(new Door(false,3),670,400);
    }
    
    public void Time()
    {
        background=new GreenfootImage("ItsTimeBackground.png");
        setBackground(background);
        bgmask=new GreenfootImage("ItsTimeMask.png");
        startx=0;
        starty=380;
        addObject(new Egg(),startx,starty);
        addObject(new ChoseStory(9,1),650,240);
        addObject(new ChoseStory(21,2),40,80);
        addObject(new ChoseStory(33,0),650,20);
    }
    //GoodGuy
    public void IntroDuction()
    {
        background=new GreenfootImage("GoodGuyVillageBackground.png");
        setBackground(background);
        bgmask=new GreenfootImage("GoodGuyVillageMask.png");
        startx=0;
        starty=400;
        addObject(new Egg(),startx,starty);
        addObject(new ChoseStory(10,3),30,50);
        addObject(new ChoseStory(11,4),460,25);
        addObject(new ChoseStory(12,5),650,140);
    }
    
    public void JgAreaLvl1To2()
    {
        background=new GreenfootImage("Lvl1-2JgAreaBackGround.png");
        setBackground(background);
        bgmask=new GreenfootImage("Lvl1-2JgArea.png");
        startx=0;
        starty=330;
        addObject(new Egg(),startx,starty);
        addObject(new ChoseStory(13,3),300,440);
        addObject(new MonsterSpawner(1),540,250);
        addObject(new MonsterSpawner(1),300,10);
    }
    
    public void JgAreaLvl2To5()
    {
        background=new GreenfootImage("GoodGuyVillageBackground.png");
    }
    //BadGuy
    public void BadIntroDuction()
    {
        
    }
    //Just... Um yah
    public void LOL()
    {
        
    }
}
Super_Hippo Super_Hippo

2017/5/25

#
The question is what you mean with "things". What kind of objects are showing up? I wouldn't be surprised if it has something to do with using all these static variables...
DuckGod DuckGod

2017/5/25

#
It's the ChoseStory class that is the phantom object and im sure i removed it from the last level, or it could be the ChoseStory class i put in has sort of glitched out and is in more than just one place.
DuckGod DuckGod

2017/5/25

#
Ok so I added another local variable to ChoseStory called required level and it checks the Egg's Level and some portals require the egg to be a higher level and so I made the ChoseStory in JgAreaLvl1To2() to be required level 3 and the Egg's level is only lvl 1 so I now don't know what is happening and if you want the code here it is for ChoseStory
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ChoseStory here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ChoseStory extends NeededStuff
{
    int whatLevel;
    int requiredLevel;
    int wait=0;
    static GreenfootImage[] Choices=new GreenfootImage[30];
    public ChoseStory(int Level, int img, int req)
    {
        whatLevel=Level;
        requiredLevel=req;
        if(Choices[0]==null)
        {
            Choices[0]=new GreenfootImage("ForeverDeathChoice.png");
            Choices[1]=new GreenfootImage("GoodGuyChoice.png");
            Choices[2]=new GreenfootImage("BadGuyChoice.png");
            Choices[3]=new GreenfootImage("Lvl1-5JgArea.png");
            Choices[4]=new GreenfootImage("Lvl6-10CaveArea.png");
            Choices[5]=new GreenfootImage("FinalLvl11-20Underworld.png");
        }
        for(int i=0;i<6;i++)
        {
            Choices[i].scale(60,70);
        }
        bCanBeAttackedFromBelow=false;
        bCanBeAttackedFromAbove=false;
        setImage(Choices[img]);
    }

    /**
     * Act - do whatever the ChoseStory wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(bRemoved){return;}
        if(wait>0){wait++;}
        if(Egg.level>=requiredLevel)
        {
            if(wait>8)
            {
                WorldLoader world=(WorldLoader) getWorld();
                bRemoved=true;
                world.currentLevel=whatLevel;
                world.setLevel(whatLevel);
            }
        }
    }  

    public void damageStun(int dmg)
    {
        if (bRemoved) {return;}
        WorldLoader world = (WorldLoader) getWorld();
        wait = 1;
    }

    public boolean canattack()
    {
        return false;
    }
}
Super_Hippo Super_Hippo

2017/5/26

#
The only way a ChosenStory object is removed from the world is by the 'cleanUp' method which is called right before a new level is created. While the level is active, they won't get removed anywhere (at least not in the code shown). Its 'bRemoved' variable might be set to 'true', but this doesn't remove the object.
DuckGod DuckGod

2017/5/30

#
but it doesn't happen to the previous level called time which also has 3 ChoseStory objects in game and all of them are successfully removed.
DuckGod DuckGod

2017/5/30

#
and It's only happening to that one lvl called JgAreaLvl1To3 because I made a new Lvl to test stuff out and when it switches to that new lvl the phantom objects aren't there
danpost danpost

2017/5/30

#
Maybe the problem is in your MonsterSpawner classs as that is the only thing different between the JgAreaLvl1To3 level and the others (please show the code to the MonsterSpawner class).
DuckGod DuckGod

2017/5/30

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

/**
 * Write a description of class MonsterSpawner here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MonsterSpawner extends NeededStuff
{
    private int SpawnerType;
    private int counter;
    
    private WorldLoader world;
    public MonsterSpawner(int type)
    {
        SpawnerType=type;
        bCanBeAttackedAtAll=false;
        setImage("empty.png");
        canattack();
    }
    
    /**
     * Act - do whatever the MonsterSpawner wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        whatToSpawn();
        counter++;
    }   
    
    public void addedToWorld(World world) {
        this.world = (WorldLoader) world;  

        x = getX();
        y = getY();
    }
    
    public void whatToSpawn()
    {
        if(SpawnerType==1)
        {
            if(counter % 1340==0)
            {
                world.addObject(new BadGuys(0),getX(),getY());
            }
            if(counter % 550==0)
            {
                world.addObject(new BadGuys(1),getX(),getY());
            }
        }
    }
    
    public boolean canattack()
    {
        return false;
    }
}
DuckGod DuckGod

2017/5/30

#
ok Ima go to sleep help me tomrrow
DuckGod DuckGod

2017/5/31

#
OK i am back
danpost danpost

2017/5/31

#
One thing you can try is changing all your background image files from ".png" format to ".jpg" format. The ".png" format supports transparencies, and the background image should not have any transparent (or semi-transparent) pixels at all. Another somewhat of an issue is the use of an actor object to spawn other actors (using 'type == 1' (or 'SpawnerType == 1'). Just have the WorldLoader class act method run the counter and spawn the BadGuys objects. Side thought: I wonder what the zero and one parameters in the BadGuys constructor is for?!
DuckGod DuckGod

2017/6/1

#
Oh, it's just abstraction to make 2 diffrent kinds of badguys
DuckGod DuckGod

2017/6/1

#
Ok so changing it to a jpg format did not fix it
danpost danpost

2017/6/1

#
DuckGod wrote...
Ok so changing it to a jpg format did not fix it
You have a different background image for each level. All should be in jpg format. At least now we can eliminate the possibility that transparency in the images was the cause of the phantom images. What do these phantom objects look like? (color, shape, etc)
There are more replies on the next page.
1
2