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

2014/9/22

HI, I am currently working on a scrolling game. Im already done with creating the world and main actor is now moving with the scroll effect. I am now on the part where i need to add some random enemies on my map but when i do so, they move along with the

angelomoloboco angelomoloboco

2014/9/22

#
.
Super_Hippo Super_Hippo

2014/9/22

#
You shouldn't put all your text in the title. It seems that the title has a maximum of characters, so your sentence was cut off.
angelomoloboco angelomoloboco

2014/9/22

#
I am currently working on a scrolling game. Im already done with creating the world and main actor is now moving with the scroll effect. I am now on the part where i need to add some random enemies on my map but when i do so, they move along with the camera, so they float on the background. I need help on how to add them on my map so they wont move when I move my main actor and also on different part of my map. Thanks.
danpost danpost

2014/9/22

#
There are three types of actors in a scrolling environment -- the main actor which is always within the window (and usually in or near the center), the static actors that always remain in the same place within the window (usually info-text like score, lives, level number, etc.) and those actors that interact with the main actor that may or may not have some movement code of their own AND are moved (scrolled) with the background image. This last thing is what you appear to be having problems with or have not included within your project at all. To allow the map to be expanded, add a fourth parameter, 'false', to your world constructor (refer to the World class API for information on this). This will remove the restriction of your actors to be maintained within world bounds and allow them to move off the screen and back. You will then be pretty much required to add code to your actors to limit their movements to within the range of your enlarged map size.
angelomoloboco angelomoloboco

2014/9/22

#
I will post my codes later for you to check. I already created the enemies for my main actor on my game that I want to move random on the screen. What my problem is when i add them, they move with my main actor like static actors.. any recommendations? thanks a lot
danpost danpost

2014/9/22

#
angelomoloboco wrote...
any recommendations?
Did you not see my post? I believe it points you in the correct direction to fix this issue.
angelomoloboco angelomoloboco

2014/9/23

#
here are my codes.. i dont know how to add my actors. Floor World public Floor() { // Create a new world with 600x400 cells with a cell //size of 1x1 pixels. super(800, 600, 1,false); makeMap(); update(); addObject(counter, 590, 54); addObject(healthbar, 689, 54); addObject(daddy, 37, 541); prepare(); } public Counter getCounter() { return counter; } public HealthBar getHealthBar() { return healthbar; } public void makeMap() { for (int y=0;y<MAPIMGHEIGHT;y++) { for (int x=0;x<MAPIMGWIDTH;x++) { int colorRGB = mapImg.getColorAt (x,y).getRGB(); if (colorRGB == Color.BLACK.getRGB()) { int mapX = x * PLATFORMWIDTH + PLATFORMWIDTH/2; int mapY = y * PLATFORMHEIGHT + PLATFORMHEIGHT/2; thePlatforms.add(new Platform(mapX,mapY)); } } } } public void shiftScreen (int changeX, int changeY) { leftBound += changeX; rightBound += changeX; if (leftBound <0) { leftBound =0; rightBound = getWidth(); } else if (rightBound >= MAPWIDTH) { rightBound = MAPWIDTH; leftBound = MAPWIDTH - getWidth(); } topBound -= changeY; bottomBound -= changeY; if (topBound <0) { topBound =0; bottomBound = getHeight(); } else if (bottomBound >= MAPHEIGHT) { bottomBound = MAPHEIGHT; topBound = MAPHEIGHT - getHeight(); } update(); } public void update() { Platform thisPlatform; int thisPlatformX; int thisPlatformY; int screenX; int screenY; for (int i=0;i<thePlatforms.size();i++) { thisPlatform = thePlatforms.get(i); thisPlatformX = thisPlatform.mapX; thisPlatformY = thisPlatform.mapY; if (thisPlatformX + PLATFORMWIDTH>=leftBound && thisPlatformX - PLATFORMWIDTH<=rightBound && thisPlatformY+PLATFORMHEIGHT >=topBound && thisPlatformY-PLATFORMHEIGHT<=bottomBound) { screenX = thisPlatformX - leftBound; screenY = thisPlatformY - topBound; if (thisPlatform.getWorld()==null) { addObject (thisPlatform, screenX, screenY); } else { thisPlatform.setLocation(screenX, screenY); } } else { if (thisPlatform.getWorld()!=null) { removeObject(thisPlatform); } } } } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { /* Potionpotion = new Potion; for (int i=0;i <potion.length;i++) { potion = new Potion(); int potionX = Greenfoot.getRandomNumber(getWidth()); int potionY = Greenfoot.getRandomNumber(600); addObject(potion, potionX, potionY); }*/ } Actor Class Counter counter = new Counter(); GreenfootSound jumpMusic = new GreenfootSound ("jumpMusic.wav"); boolean inTheAir = false; int groundHeight = getImage().getHeight()/2; int sideWidth = getImage().getWidth()/2; int deltaX = 0; int deltaY = 0; World myWorld; int worldHeight; int worldWidth; boolean touchingHuman = false; public void addedToWorld (World theWorld) { myWorld = theWorld; worldHeight = myWorld.getHeight(); worldWidth = myWorld.getWidth(); } public void act() { Actor potion = getOneIntersectingObject(Potion.class); if (potion !=null) { World myWorld = getWorld(); myWorld.removeObject(potion); Floor floor = (Floor)myWorld; Counter counter = floor.getCounter(); counter.addScore(); } moveMent(); move(); hitHuman(); leftNright(); } public void moveMent() { if(inTheAir) { fall(); }else { if (Greenfoot.isKeyDown ("left")) { deltaX = -4; } else if (Greenfoot.isKeyDown ("right")) { deltaX = 4; }else { deltaX = 0; } if (Greenfoot.isKeyDown("space")) { deltaY +=10; jumpMusic.play(); jumpMusic.setVolume(95); } } } public void move() { int newX = getX() + deltaX; int newY = getY() - deltaY; Actor platformBelow = getOneObjectAtOffset(0, groundHeight+4,Platform.class); Actor platformAbove = getOneObjectAtOffset(0, -groundHeight-4,Platform.class); if (platformBelow !=null) { if (deltaY<0) { deltaY = 0; inTheAir = false; GreenfootImage platformImage = platformBelow.getImage(); int topOfPlatform = platformBelow.getY() - platformImage.getHeight()/2; newY = topOfPlatform - groundHeight; } } else if (getY() >= worldHeight - groundHeight) { if (deltaY <0) { deltaY = 0; inTheAir = false; newY = worldHeight - groundHeight; } } else { inTheAir = true; } if (platformAbove !=null) { if (deltaY>0) { deltaY = 0; GreenfootImage platformImage = platformAbove.getImage(); int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight()/2; newY = bottomOfPlatform + groundHeight; } } if (getX() <= sideWidth) { deltaX = Math.abs(deltaX); } if (getX() >= worldWidth-sideWidth) { deltaX = Math.abs(deltaX)*-1; } setLocationWithScroll(newX, newY); } public void leftNright() { int newX = getX() + deltaX; int newY = getY() - deltaY; Actor platformToRight = getOneObjectAtOffset(sideWidth +4, 0,Platform.class); Actor platformToLeft = getOneObjectAtOffset(-sideWidth -4, 0,Platform.class); if (platformToRight !=null) { deltaX = Math.abs(deltaX)*-1; } if (platformToLeft !=null) { deltaX = Math.abs(deltaX); } setLocationWithScroll(newX, newY); } public void fall() { deltaY--; } public void setLocationWithScroll (int newX, int newY) { Floor myFloor = (Floor)getWorld(); if ((newY<200 && myFloor.topBound>0) || (newY > worldHeight-200&& myFloor.bottomBound < myFloor.MAPHEIGHT)) { int yShift = newY - getY(); myFloor. shiftScreen(0, -yShift); }else { setLocation(getX(), newY); } if ((newX<350 && myFloor.leftBound >0) || (newX>worldWidth-350 && myFloor.rightBound < myFloor.MAPWIDTH)) { int xShift = newX - getX(); myFloor.shiftScreen(xShift,0); }else { setLocation(newX, getY()); } } i dont know how to add some random actors on the world that will go left and disappears while my actor go right. Thank you in advance :)
Super_Hippo Super_Hippo

2014/9/23

#
Please use the 'code' tags to post code. This way, you won't create italic texts when using an index i in array brackets for example and it is much better to read.
angelomoloboco angelomoloboco

2014/9/25

#
here are my codes.. i dont know how to add my actors.
Floor World
    public Floor()
    {    
        // Create a new world with 600x400 cells with a cell 
        //size of 1x1 pixels.
        super(800, 600, 1,false); 
        makeMap();
        update();
        
        addObject(counter, 590, 54);
        addObject(healthbar, 689, 54);
        addObject(daddy, 37, 541);

        prepare();
    }
    public Counter getCounter()
    {
        return counter;
    }
    public HealthBar getHealthBar()
    {
        return healthbar;
    }

    public void makeMap()
    {
        for (int y=0;y<MAPIMGHEIGHT;y++)
        {
            for (int x=0;x<MAPIMGWIDTH;x++)
            {
                int colorRGB = mapImg.getColorAt (x,y).getRGB();
                if (colorRGB == Color.BLACK.getRGB())
                {
                    int mapX = x * PLATFORMWIDTH + PLATFORMWIDTH/2;
                    int mapY = y * PLATFORMHEIGHT + PLATFORMHEIGHT/2;
                    thePlatforms.add(new Platform(mapX,mapY));

                }
            }
        }
    }

    public void shiftScreen (int changeX, int changeY)
    {
        leftBound += changeX;
        rightBound += changeX;
        if (leftBound <0)
        {
            leftBound =0;
            rightBound = getWidth();
        }
        else if (rightBound >= MAPWIDTH)
        {
            rightBound = MAPWIDTH;
            leftBound = MAPWIDTH - getWidth();
        }

        topBound -= changeY;
        bottomBound -= changeY;
        if (topBound <0)
        {
            topBound =0;
            bottomBound = getHeight();
        }
        else if (bottomBound >= MAPHEIGHT)
        {
            bottomBound = MAPHEIGHT;
            topBound = MAPHEIGHT - getHeight();
        }
        update();
    }

    public void update()
    {
        Platform thisPlatform;
        int thisPlatformX;
        int thisPlatformY;
        int screenX;
        int screenY;
        for (int i=0;i<thePlatforms.size();i++)
        {
            thisPlatform = thePlatforms.get(i);
            thisPlatformX = thisPlatform.mapX;
            thisPlatformY = thisPlatform.mapY;

            if (thisPlatformX + PLATFORMWIDTH>=leftBound && thisPlatformX - PLATFORMWIDTH<=rightBound && thisPlatformY+PLATFORMHEIGHT >=topBound && thisPlatformY-PLATFORMHEIGHT<=bottomBound)
            {
                screenX = thisPlatformX - leftBound;
                screenY = thisPlatformY - topBound;
                if (thisPlatform.getWorld()==null)
                {
                    addObject (thisPlatform, screenX, screenY);
                }
                else
                {
                    thisPlatform.setLocation(screenX, screenY);
                }
            } 
            else 
            {
                if (thisPlatform.getWorld()!=null)
                {
                    removeObject(thisPlatform);
                }
            }

        }
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
       /* Potionpotion = new Potion;
        for (int i=0;i <potion.length;i++)
        {
            potion = new Potion();
            int potionX = Greenfoot.getRandomNumber(getWidth());
            int potionY = Greenfoot.getRandomNumber(600);
            addObject(potion, potionX, potionY);
        }*/
        
    }

Actor Class


Counter counter = new Counter();
    GreenfootSound jumpMusic = new GreenfootSound ("jumpMusic.wav");
    boolean inTheAir = false;
    int groundHeight = getImage().getHeight()/2;
    int sideWidth = getImage().getWidth()/2;
    int deltaX = 0;
    int deltaY = 0;
    World myWorld;
    int worldHeight;
    int worldWidth;
    boolean touchingHuman = false;

    public void addedToWorld (World theWorld)
    {
        myWorld = theWorld;
        worldHeight = myWorld.getHeight();
        worldWidth = myWorld.getWidth();
    }

    public void act() 
    {
        Actor potion = getOneIntersectingObject(Potion.class);
        if (potion !=null)
        {
            World myWorld = getWorld();
            myWorld.removeObject(potion);
            Floor floor = (Floor)myWorld;
            Counter counter = floor.getCounter();
            counter.addScore();
        }
        moveMent();
        move();
        hitHuman();
        leftNright();
    }

    public void moveMent()
    {
        if(inTheAir)
        {
            fall();
        }else
        {
            if (Greenfoot.isKeyDown ("left"))
            {
                deltaX = -4;
            }
            else if (Greenfoot.isKeyDown ("right"))
            {
                deltaX = 4;
            }else
            {
                deltaX = 0;
            }
            if (Greenfoot.isKeyDown("space"))
            {
                deltaY +=10;

                jumpMusic.play();
                jumpMusic.setVolume(95);
            }
        }
    }    

    public void move()
    {

        int newX = getX() + deltaX;
        int newY = getY() - deltaY;
        Actor platformBelow = getOneObjectAtOffset(0, groundHeight+4,Platform.class);
        Actor platformAbove = getOneObjectAtOffset(0, -groundHeight-4,Platform.class);
        
        if (platformBelow !=null)
        {
            if (deltaY<0)
            {
                deltaY = 0;
                inTheAir = false;
                GreenfootImage platformImage = platformBelow.getImage();
                int topOfPlatform = platformBelow.getY() - platformImage.getHeight()/2;
                newY = topOfPlatform - groundHeight;
            }
        }
        else if (getY() >= worldHeight - groundHeight)
        {
            if (deltaY <0)
            {
                deltaY = 0;
                inTheAir = false;
                newY = worldHeight - groundHeight;
            }
        }
        else
        {
            inTheAir = true;
        }
        if (platformAbove !=null)
        {
            if (deltaY>0)
            {
                deltaY = 0;
                GreenfootImage platformImage = platformAbove.getImage();
                int bottomOfPlatform = platformAbove.getY() + platformImage.getHeight()/2;
                newY = bottomOfPlatform + groundHeight;
            }
        }
        if (getX() <= sideWidth)
        {
            deltaX = Math.abs(deltaX);
        }
        if (getX() >= worldWidth-sideWidth)
        {
            deltaX = Math.abs(deltaX)*-1;
        }
        setLocationWithScroll(newX, newY);
    }
    public void leftNright()
    {
        int newX = getX() + deltaX;
        int newY = getY() - deltaY;
        Actor platformToRight = getOneObjectAtOffset(sideWidth +4, 0,Platform.class);
        Actor platformToLeft = getOneObjectAtOffset(-sideWidth -4, 0,Platform.class);
        if (platformToRight !=null)
        {
            deltaX = Math.abs(deltaX)*-1;
        }
        if (platformToLeft !=null)
        {
            deltaX = Math.abs(deltaX);
        }

        setLocationWithScroll(newX, newY);

    }

    public void fall()
    {
        deltaY--;
    }

    public void setLocationWithScroll (int newX, int newY)
    {
        Floor myFloor = (Floor)getWorld();
        if ((newY<200 && myFloor.topBound>0) || (newY > worldHeight-200&& myFloor.bottomBound < myFloor.MAPHEIGHT))
        {
            int yShift = newY - getY();
            myFloor. shiftScreen(0, -yShift);
        }else
        {
            setLocation(getX(), newY);   
        }
        if ((newX<350 && myFloor.leftBound >0) || (newX>worldWidth-350 && myFloor.rightBound < myFloor.MAPWIDTH))
        {
            int xShift = newX - getX();
            myFloor.shiftScreen(xShift,0);
        }else
        {
            setLocation(newX, getY());  
        }

    }


i dont know how to add some random actors on the world that will go left and disappears while my actor go right. Thank you in advance :)
Super_Hippo Super_Hippo

2014/9/25

#
Well, to add an actor, you call 'addObject' on the world object. To let it spawn on a random position, you can use 'Greenfoot.getRandomNumber'. And to let them go to the left, you can use 'setLocation( getX()-1, getY())' in the 'act' method of the spawned actor.
angelomoloboco angelomoloboco

2014/9/26

#
can u show me the codes please.?
Super_Hippo Super_Hippo

2014/9/26

#
What exactly didn't you understand?
danpost danpost

2014/9/26

#
danpost wrote...
There are three types of actors in a scrolling environment -- the main actor which is always within the window (and usually in or near the center), the static actors that always remain in the same place within the window (usually info-text like score, lives, level number, etc.) and those actors that interact with the main actor that may or may not have some movement code of their own AND are moved (scrolled) with the background image. This last thing is what you appear to be having problems with or have not included within your project at all. < cont'd >
When I create a scroller, I use what I call a 'move and shift' method. I let the main actor move normally (with standard movement code in the class of the main actor), then let the world shift all the actors and the background so that the main actor is back within its bounds (or in the center of the window).
You need to login to post a reply.