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

2016/11/24

How to save and intersecting object?

nathe2000 nathe2000

2016/11/24

#
I am making a game where a "knight" will go up a lane and attack another object, like Clash Royal. My issue is that I need it to attack and the way that I thought of to do it was to save the object that the knight intersected with first, then have another method that would get that object and reduce its health. I don't know how to save the object that the knight intersected and how to get its health variable. This is my code so far...
 public void attack()
    {
       //possibly make the attack here? 
    }
    public void playerMove()    //move the player if it doesn't intersect another actor
    {
        moveTimer++;    //time to wait to move
        Actor c = (Actor)getOneIntersectingObject(Actor.class); //get intersecting object
        if(moveTimer == 20) //how long until it acts, 20 act cycles
        {  
            if(c == null)   //if it hasn't touched anything
            {
                setLocation(getX(), getY() - 1);    //move up one cell
                moveTimer = 0;  //reset timer to act again
            }
            else    //if it has touched something
            {
                moveTimer = 0;  //reset timer to act again
            }
        }
    }
Any help would be appreciated. Thanks!
You need to login to post a reply.