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

2018/8/19

Wall

Dr.Dre Dr.Dre

2018/8/19

#
hi guys I'm trying to make a game just like the atari classic arcade but how can I make a wall at the edges of the screen smth. like if(playerthoucheswall) { don'tpass }
Super_Hippo Super_Hippo

2018/8/19

#
temporarily save current location move if touching a wall, move back to saved location
Dr.Dre Dr.Dre

2018/8/19

#
Thanks but could you please show ho the code would look like
Super_Hippo Super_Hippo

2018/8/20

#
int x=getX(), y=getY();
//-- insert moving code here
if (isTouching(Wall.class)) setLocation(x, y);
danpost danpost

2018/8/20

#
Super_Hippo wrote...
<< Code Omitted >>
I take it no walls are at "edges of screen". For vertical edges:
int halfWidth = getImage().getWidth()/2;
if (getX() < halfWidth) setLocation(halfWidth, getY());
if (getX() > getWorld().getWidth()-halfWidth) setLocation(getWorld().getWidth()-halfWidth, getY());
Similar code (using heights and y-coordinate) would be used for the horizontal edges.
Agent40 Agent40

2018/8/21

#
In order for an object to not pass through an object of any kind you, it needs to receive the equal to opposite amount of force. This would be placed in the same area that the movement code is for each direction possible (or you can use a boolean to switch the values to add a bounce affect if it's an AI/Enemy).
//Reference 
Actor ActorName = getOneIntersectingObject(ActorName.class); 
            if (ActorName != null) // Checks if the Player/Enemy is touching the wall                                     
                setLocation(getX(), getY()); //Change to the opposite of the regular speed (i.e. getX + 6 turns into getX - 6)


//Example IF Wall is above (this piece of code goes into the same method allowing the actor to move in the first place)
Actor Wall = getOneIntersectingObject(Wall.class);
            if (Wall != null)                                
                setLocation(getX(), getY() + Speed); //Speed can be a Variable or straight integer (number)


//Example IF It's an AI/Enemy (assumes movement is set in a method and assume bounce is only up and down)
private boolean Bounced = false;
...
if (Bounced == false) //Moving up
{
     setLocation(getX(), getY() - Speed); 
     Actor Wall = getOneIntersectingObject(Wall.class);
                if (Wall != null)                                
                    Bounced = true;
}
else //Moving Down
{
     setLocation(getX(), getY() + Speed); 
     Actor Wall = getOneIntersectingObject(Wall.class); //can be replaced to check for Y = 0;
                if (Wall != null)                                
                    Bounced = false;
}
If you want to make a position in the world a wall however instead of using a class then just tell me and I'll mock some code up.
You need to login to post a reply.