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

4 days ago

removing object from array on world by clicking at it

1
2
3
Zurah Zurah

4 days ago

#
the world generation works again (i guess they were still set too high), but i still cant move weirdly enough
danpost danpost

4 days ago

#
Zurah wrote...
the world generation works again (i guess they were still set too high), but i still cant move weirdly enough
Show Spieler class codes.
Zurah Zurah

4 days ago

#
its empty (as empty as a new class is). should i move the movement code from MyWorld.class into Spieler.class? edit: tried that approach and it doesnt work
danpost danpost

4 days ago

#
Zurah wrote...
its empty (as empty as a new class is). should i move the movement code from MyWorld.class into Spieler.class? edit: tried that approach and it doesnt work
You could put this line in act of Spieler class:
setLocation(MyWorld.playerX, MyWorld.playerY);
Zurah Zurah

4 days ago

#
player movement works again, but now the "camera" doesnt follow the player anymore. i intended it to be centered on the player (so that the player is always in the same position on the screen) but can still freely move around the map
danpost danpost

4 days ago

#
Zurah wrote...
player movement works again, but now the "camera" doesnt follow the player anymore. i intended it to be centered on the player (so that the player is always in the same position on the screen) but can still freely move around the map
Oh !!! There was no indication that scrolling was to be involved here. Your world is currently bounded. I see now why you had the world generation code in the act method. Due to the possible number of actor in your world, recreating all the actors every act step would seem the right approach. Revert back to the "sorry, I miisunderstood u" code and give me some time to come up with a fix.
Zurah Zurah

4 days ago

#
sure, no worries ^^ it was a mistake of me not to indicate that the world was planned to be designed that way ^^
danpost danpost

3 days ago

#
Zurah wrote...
sure, no worries ^^ it was a mistake of me not to indicate that the world was planned to be designed that way ^^
Okay. I wrote a short scenario to get the scrolling to work properly. What I came up with follows (some things are not named similarly or sized like yours). Main thing for you is setup, genActor, act and scroll methods, plus the reference field to the player. Here it is:
import greenfoot.*;

public class MyWorld extends World
{
    static final int WIDE = 27, HIGH = 15, CELL = 40;
    static final int BIG_X = 50, BIG_Y = 30;
    
    int[][] grid = new int[BIG_Y][BIG_X];
    int scrollX, scrollY;
    Actor player = new Player();
    
    public MyWorld()
    {
        super(WIDE, HIGH, CELL);
        setPaintOrder(Player.class);
        setup();
        addObject(player, WIDE/2, HIGH/2);
    }
    
    /** Populates the world */
    private void setup()
    {
        // building grid
        for (int y=8; y<BIG_Y; y++) for (int x=0; x<BIG_X; x++) grid[y][x] = 1;
        for (int y=10; y<BIG_Y; y++) for (int x=0; x<BIG_X; x++)
            if (Greenfoot.getRandomNumber(5) == 0) grid[y][x] = 2;
        // populating world as per grid
        for (int y=0; y<HIGH; y++) for (int x = 0; x<WIDE; x++) genActor(x, y);
    }
    
    /** Adds an actor at given location if one should exist there */
    private void genActor(int x, int y)
    {
        switch(grid[y+scrollY][x+scrollX])
        {
            case 1: addObject(new Dirt(), x, y); break;
            case 2: addObject(new Mineral(2), x, y); break;
        }
    }
    
    /** Performs player movement and scrolling */
    public void act()
    {
        // check moving
        int dx = 0, dy = 0;
        if (Greenfoot.isKeyDown("D")) dx++;
        if (Greenfoot.isKeyDown("A")) dx--;
        if (Greenfoot.isKeyDown("S")) dy++;
        if (Greenfoot.isKeyDown("W")) dy--;
        if (dx*dy != 0 || dx+dy == 0) return;
        // do no more if trying to move past edge
        int x = player.getX(), y = player.getY();
        if (x+dx < 0 || x+dx > WIDE-1) return;
        if (y+dy < 0 || y+dy > HIGH-1) return;
        // move player
        player.setLocation(player.getX()+dx, player.getY()+dy);
        // scroll, if able
        if (!(dx == -1 && scrollX == 0) &&
            !(dy == -1 && scrollY == 0) &&
            !(dx == 1 && scrollX == BIG_X-WIDE) &&
            !(dy == 1 && scrollY == BIG_Y-HIGH)) scroll(dx, dy);
        // time delay when moving
        Greenfoot.delay(20);
    }
    
    /** Scrolls the actors, if needed (removing and adding as required) */
    private void scroll(int dx, int dy)
    {
        // do not scroll if player is not moving away from center
        if (dx == 1 && player.getX() <= WIDE/2) return;
        if (dx == -1 && player.getX() >= WIDE/2) return;
        if (dy == 1 && player.getY() <= HIGH/2) return;
        if (dy == -1 && player.getY() >= HIGH/2) return;
        // scroll all actor (including player -- toward center)
        for (Object obj : getObjects(null))
        {
            Actor actor = (Actor) obj;
            int newX = actor.getX()-dx, newY = actor.getY()-dy;
            actor.setLocation(newX, newY);
            // remove the actor if leaving world view
            if (newX < 0 || newX > WIDE-1 || newY < 0 || newY > HIGH-1)
                removeObject(actor);
        }
        // adjust scroll values
        scrollX += dx;
        scrollY += dy;
        // adds actors coming into world view
        if (dx == 1) for (int y=0; y<HIGH; y++) genActor(WIDE-1, y);
        if (dx == -1) for (int y=0; y<HIGH; y++) genActor(0, y);
        if (dy == 1) for (int x=0; x<WIDE; x++) genActor(x, HIGH-1);
        if (dy == -1) for (int x=0; x<WIDE; x++) genActor(x, 0);
    }
}
You should only need to change the values in lines 5 and 6 to adjust sizes.
Zurah Zurah

3 days ago

#
alright, im gonna try to implement this and see where it goes ^^
Zurah Zurah

3 days ago

#
thank you so much for your work ^^ everything is working fine now the only functionality i lost is collision with other objects in the world. i tried my old approach by using
if(Greenfoot.isKeyDown("S") && world[playerX][playerY + 1] == 0)...
but that resulted in a crash because line 45 defined playerX and playerY (renamed those) as 0, so if it subtracts one, it will go into negative causing a out of bounds crash. if you by chance know of a fix for this (perhaps using the getOneObjectAtOffset() method) let me know ^^ but again, thanks already so much for all the quick help provided, i hope it didnt take you too much time haha
danpost danpost

3 days ago

#
Zurah wrote...
if(Greenfoot.isKeyDown("S") && world[playerX][playerY + 1] == 0)...
Add the needed extra condition:
if (Greenfoot.isKeyDown("S") && getY() < 15 && world[playerX}[playerY + 1] == 0)...
Zurah Zurah

3 days ago

#
what exactly is "getY" in that case?
danpost danpost

3 days ago

#
Zurah wrote...
what exactly is "getY" in that case?
Where, exactly?
Zurah Zurah

3 days ago

#
if (Greenfoot.isKeyDown("S") && getY() < 15 && world[playerX}[playerY + 1] == 0)...
the getY() < 15 part. since i renamed some of the variables, i dont fully understand what it means in ur version of the code
danpost danpost

3 days ago

#
Zurah wrote...
if (Greenfoot.isKeyDown("S") && getY() < 15 && world[playerX}[playerY + 1] == 0)...
the getY() < 15 part. since i renamed some of the variables, i dont fully understand what it means in ur version of the code
Oh. Use "playerY" instead. Also, should be "< 14".
There are more replies on the next page.
1
2
3