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

2021/1/10

How can I add invisible Walls?

Sakuya Sakuya

2021/1/10

#
Im making a Touhou parody in Greenfoot but my Charackter can fly to the scoreboard is there a code where I can adjust the space where all charackter are able to spawn/move?
FirewolfTheBrave FirewolfTheBrave

2021/1/10

#
I don't know what your project will exactly look like, but let's assume you're going to restrict your character's spawning and movement to the area between x=100 and x=300 horizontally and y=100 and y=300 vertically. To restrict the spawning area, use this code in the spawning method (if it's in an Actor subclass, add getWorld(). between the this. and AddObject()):
this.addObject(new <class name>, Greenfoot.getRandomNumber(200) + 100, Greenfoot.getRandomNumber(200) + 100);
To restrict the character movement, use this code in the character's movement method:
int newX = <code to calculate the next x-coordinate>;
int newY = <code to calculate the next  y-coordinate>;
if (newX > 100 && newX < 300 && newY > 100 && newY < 300) this.setLocation(newX, newY);
As I mentioned, his code will restrict your character to the area between x=100 and x=300 horizontally and y=100 and y=300 vertically. You can adjust the numbers however you want.
Sakuya Sakuya

2021/1/11

#
Ok but what do you write where you tipped code to calculate the next x/y coordinates ?
danpost danpost

2021/1/11

#
Sakuya wrote...
Ok but what do you write where you tipped code to calculate the next x/y coordinates ?
That would depend on the individual types of Actor and how the instances of each move.
Sakuya Sakuya

2021/1/11

#
I dont quite understand im new to this so I tryed to use the code but it dosnt work my world is scaled 800x600 and I kinda want the charackters movement to be at 50-500 x and 50-550 y. I dont fully understandt what you mean but i can move my charackter with wasd around the whole world and i want his movement to be scaled in the lenght I named. I hope anything I wrote made sense, sorry english is not my native language and I dont fully understand what you guys mean
danpost danpost

2021/1/11

#
Sakuya wrote...
I kinda want the charackters movement to be at 50-500 x and 50-550 y. ... but i can move my charackter with wasd around the whole world
First, collect key data to determine intended direction of movement along horizontal and vertical:
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--;
Next, for 4-way movement, allow only one direction at a time and check range of movement:
if (dx*dy == 0 && dx+dy != 0 // one, and only one, direction is not zero
    && getX()+dx*speed >= 50 && getX()+dx*speed <= 500 // new x allowable
    && getY()+dy*speed >= 50 && getY()+dy*speed <= 550) // new y allowable
{
    setLocation(getX()+dx*speed, getY()+dy*speed);
}
FirewolfTheBrave FirewolfTheBrave

2021/1/11

#
Alright, then we'll need to change the code up slightly. Remove your current movement code, then put this bit of code into your character class:
public void move()
{
     int newX = this.getX();
     int newY = this.getY();
     if (Greenfoot.isKeyDown("w")) newY--;
     else if (Greenfoot.isKeyDown("a")) newX--;
     else if (Greenfoot.isKeyDown("s")) newY++;
     else if (Greenfoot.isKeyDown("d")) newX++;
     if (newX > 50 && newX < 500 && newY > 50 && newY < 500) this.setLocation(newX, newY);
}
Call this method by writing
move();
into your act() method. Remove your current spawning code, and put this code into your world constructor:
this.addObject(new <class name>, Greenfoot.getRandomNumber(450) + 50, Greenfoot.getRandomNumber(450) + 50);
This will spawn one character at the very beginning of the game. If you don't like that, leave a reply and I'll change the code for you. Edit: I see that danpost has also replied. Both codes seem to work, so you can pick one.
Sakuya Sakuya

2021/1/12

#
Thanks, it worked. Cant thank you both enough
You need to login to post a reply.