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

2013/2/17

getIntersectingObject

1
2
3
4
5
DJ_KASKO DJ_KASKO

2016/4/20

#
Hey guys i need help with this assignment at school. Im in year 9 and for the assignment we have to make a two player game on greenfoot. What i really need help with is collision detection. I am a beginner so i dont know much code but some basics so this collision detection is a real problem for me. So what my game is ment to be is a simple two player game with a white background and two fist facing each other. Player1 is on the left side at the end and player two the complete opposite (on the right side). There is a face in the middle of the background with the fists pointing at it. The face is KimJongUn (just to let you know). there is ment two be two counters one above Player1 and two above player2. and there is ment to be a timmer ant the top middle of the background witch i havnt done yet. and the aim of the game is who can hit KimJongUn the most times in 30 seconds. who ever gets the most hits wins. and the controls are simple. Player1's controls = UpKey(upwardsarrow) and player2's is the "w" button. but the problem is when i press up for player 1 it only go's half way through KimJongUn and stops and does not count towards the counter. and the game stops. so what i need help with is the collision from the fist and kimjongun to stop when it hits him and bounce back to its starting position. HERE is the code i have for player1 and please help me with this: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player1 here. * * @author (your name) * @version (a version number or a date) */ public class Player1 extends Animal { private Counter counter; public Player1(Counter pointCounter) { counter=pointCounter; } public void act() { checkKeys(); } /** * Act - do whatever the Player1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void checkKeys() { if (Greenfoot.isKeyDown("up") ) { move(75); } if (getX()>400) setLocation(400, getY()); } private Player1 getIntersectingPlayer1() { return (Player1) getOneIntersectingObject(Player1.class); } } Please tell me what to do and show the code to fix it. I need help with in today and the 27th of april 2016 because my assignment is due a couple of days after that Thanks guys :).
alexanderwrobinson alexanderwrobinson

2016/4/20

#
does it need to be animated when it moves backwards to its starting position? and what is the objective of the game
danpost danpost

2016/4/20

#
You are showing a 'getIntersectingPlayer1' method in the Player1 class. First, it is a 'private' method which means in can only be called from this class; but, I do not see anywhere from which it is called. Another thing is, if you only have one Player1 object in the world, why would you want to find a Player1 object that intersects the Player1 object the code is executing for. Maybe you need an intersecting method for a KimJongUn object, but it would still need to be called from somewhere to make it useful. Even then, however, I cannot say if you need for the method. The movement code for the player is incomplete. Maybe you do not realize that the entire act method is executed about 50 times a second. The code in it describes what the actor should do at any moment of time. It is not a sequence that needs to followed over a period of time.
DJ_KASKO DJ_KASKO

2016/4/21

#
alexanderwrobinson wrote...
does it need to be animated when it moves backwards to its starting position? and what is the objective of the game
no it doesnt
DJ_KASKO DJ_KASKO

2016/4/21

#
danpost wrote...
You are showing a 'getIntersectingPlayer1' method in the Player1 class. First, it is a 'private' method which means in can only be called from this class; but, I do not see anywhere from which it is called. Another thing is, if you only have one Player1 object in the world, why would you want to find a Player1 object that intersects the Player1 object the code is executing for. Maybe you need an intersecting method for a KimJongUn object, but it would still need to be called from somewhere to make it useful. Even then, however, I cannot say if you need for the method. The movement code for the player is incomplete. Maybe you do not realize that the entire act method is executed about 50 times a second. The code in it describes what the actor should do at any moment of time. It is not a sequence that needs to followed over a period of time.
so HOW do i code this correctly so that it does what i want it to do. Please write the code in a post because i still dont know what to do Oh and thank you for helping. also im a begginer so i have know idea what your saying for most of it so please just write the code because i have no clue what to do. Thank You So Much.
DJ_KASKO DJ_KASKO

2016/4/21

#
danpost wrote...
You are showing a 'getIntersectingPlayer1' method in the Player1 class. First, it is a 'private' method which means in can only be called from this class; but, I do not see anywhere from which it is called. Another thing is, if you only have one Player1 object in the world, why would you want to find a Player1 object that intersects the Player1 object the code is executing for. Maybe you need an intersecting method for a KimJongUn object, but it would still need to be called from somewhere to make it useful. Even then, however, I cannot say if you need for the method. The movement code for the player is incomplete. Maybe you do not realize that the entire act method is executed about 50 times a second. The code in it describes what the actor should do at any moment of time. It is not a sequence that needs to followed over a period of time.
I have two players (Player1 and Player2) but i also have a class KimJongUn who does not have any code in it.
danpost danpost

2016/4/21

#
What I mean is you need something to check for collision with a KimJongUn object, not a Player1 object. You really do not need any type of specialized collision method in the class. You can just use 'isTouching(KimJongUn.class)' in the class of the players to determine if touching it. Use it as a conditional in an 'if' statement, the block in which you move the player back and add to the counter:
if (isTouching(KimJongUn.class))
{
    setLocation(<? x ?>, <? y ?>);
    counter.add(1);
}
DJ_KASKO DJ_KASKO

2016/4/21

#
danpost wrote...
What I mean is you need something to check for collision with a KimJongUn object, not a Player1 object. You really do not need any type of specialized collision method in the class. You can just use 'isTouching(KimJongUn.class)' in the class of the players to determine if touching it. Use it as a conditional in an 'if' statement, the block in which you move the player back and add to the counter:
if (isTouching(KimJongUn.class))
{
    setLocation(<? x ?>, <? y ?>);
    counter.add(1);
}
Ok thanks so i delete if (getX()>400) setLocation(400, getY()); } private Player1 getIntersectingPlayer1() { return (Player1) getOneIntersectingObject(Player1.class); } that part and then i replace it with yours on player 1 and will it work. please copy my code and then replace it with whats write so i know where to put it Thank you for helping me.
danpost danpost

2016/4/21

#
You probably just need to put it where you deleted the line from. The '<x>' and '<y>' should probably be the same as that deleted line also.
DJ_KASKO DJ_KASKO

2016/4/22

#
that didint work it has a bunch of errors and i dont know how to fix them can i just give you the game and can you please help me work on it because that would be alot easier ill give you the link to my scenario that im about to post and please work on it try to fix it and make it how i want it to be and you should know what it should look like and then post the finished scenario, respond back here and give me the link to the scenario and tell me what you did and how because i want to learn as well thanks. also dont make it to complicated because im not that intelligent and up to date with where you are at the momment thank you to dan post and thank you for helping me still.
danpost danpost

2016/4/22

#
Do not remove any code. Just comment out the problem lines until it compiles. That way we can go over the errors one by one so you can learn from them.
DJ_KASKO DJ_KASKO

2016/4/22

#
ok
DJ_KASKO DJ_KASKO

2016/4/22

#
this is what the code looks like
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player1 extends Animal
{
    private Counter counter;

    public Player1(Counter pointCounter)
    {
        counter=pointCounter;
    }

    public void act()
    {
        checkKeys();
    }

    /**
     * Act - do whatever the Player1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("up") )
        {
            move(75);
        }
        if (getX()>400) setLocation(400, getY());
    }

    private Player1 getIntersectingPlayer1() 
    {
        return (Player1) getOneIntersectingObject(Player1.class);
    }

    if (isTouching(KimJongUn.class))
    {
        setLocation(<? x ?>, <? y ?>);
        counter.add(1);
    }
}
so tell me what to do next please and how to fix it
DJ_KASKO DJ_KASKO

2016/4/22

#
the bold parts are going to be the wrong (red) parts
    [b]if[/b] (isTouching[b](Ki[/b]mJongUn[b].cl[/b]ass[b]))[/b]
   [b] {[/b]
        setLocation[b](<?[/b] x ?>[b], [/b[b]]<? y ?[/b]>);
        counter.add[b](1[/b]);
 [b]   }[/b]
}
so basically nearly everything is wron in that code but the wrest of the whole code is fine
danpost danpost

2016/4/22

#
First, remove lines 36 through 39 (the 'getIntersectingPlayer1' method). A player does not need to check if it is intersecting with itself. Then, lines 41 through 45 need to be within a method. Move them to inside the 'act' method. Replace '<? x ?>' with '400' and replace '<? y ?>' with 'getY()'.
There are more replies on the next page.
1
2
3
4
5