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

2015/10/21

How do I make traffic collide?

AngelusNeko13 AngelusNeko13

2015/10/21

#
hey guys im making a game where you drive up a road and dodge traffic like the same logic as Spy Hunter the arcade game, anyways i want to make the car actor make a crashing sound and end the game when it hits the vehicle class. So far the code I typed in doesn't do anything and the sound doesn't play, can anyone please help me? here's my code so far:
    public void act() 
    {
          backgroundMusic.playLoop();
          backgroundMusic.setVolume(92);

       
        if(Greenfoot.isKeyDown("right"))
        {
            moveRight();
            
        }

        if(Greenfoot.isKeyDown("left"))
        {
            moveLeft();
        }
        if (Greenfoot.isKeyDown("up"))
        {
            moveUp();
             
        }
        if (Greenfoot.isKeyDown("down"))
        {
            moveDown();
             
        }

    }
     public void checkCollision()
    {
        
       Actor Car = getOneIntersectingObject(Vehicle.class);
        
        if(Car != null)
        
        {
            Greenfoot.playSound("crash.wav");
            Greenfoot.stop();
            
        }
    }
    public void moveRight()
    {
        setLocation(getX()+3, getY());
    }


    public void moveLeft()
    {
        setLocation(getX()-3, getY());

    }

    public void moveUp()
    {
        setLocation(getX(), getY()-2);
    }

    public void moveDown()
    {
        setLocation(getX(), getY()+2);
    }
   
        }
    
danpost danpost

2015/10/21

#
Yeah, I though that the 'checkCollision' method might not currently be called from the 'act' method.
AngelusNeko13 AngelusNeko13

2015/10/21

#
sooo what do i do?
danpost danpost

2015/10/21

#
AngelusNeko13 wrote...
sooo what do i do?
How about this: call it from the 'act' method.
AngelusNeko13 AngelusNeko13

2015/10/21

#
mkay can i have an example?
Super_Hippo Super_Hippo

2015/10/21

#
For example line 9. It calls the 'moveRight' method from the 'act' method.
AngelusNeko13 AngelusNeko13

2015/10/21

#
hey nvm i got it :) but thank you all so much this really helps for obstacles like trees, road signs. etc. but really thank you all
You need to login to post a reply.