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

2018/11/29

Score counter broken on level two, works fine on level one

1
2
3
jonah420123 jonah420123

2018/11/29

#
So i'm happy with the bike not colliding and dying with its OWN trail, i just would like it to do something when it collides with the other bikes, depending on the level, greenfoot.stop or goto level two for example. and yes it is like that but with a few tweaks to make it my own.
danpost danpost

2018/11/29

#
jonah420123 wrote...
So i'm happy with the bike not colliding and dying with its OWN trail, i just would like it to do something when it collides with the other bikes, depending on the level, greenfoot.stop or goto level two for example.
What code have you tried? and where was it tried at?
jonah420123 jonah420123

2018/11/29

#
This part controls the movement and the trail, which is an object that just continually spawns in after the bike depending on the direction it is going. A weird thing is, in previous versions of my game I had it working but all of a sudden it stopped working. Anyway;
    public void checkKeys()
    {
        
        int rotation = getRotation();
        int xPos = getX();
        int yPos = getY();
        switch(rotation)
        {    
            case 0:
                xPos = getX();
                yPos = getY();
                xPos -= 1;
                getWorld().addObject(new trail(), xPos, yPos);
                break;
            case 90:
                xPos = getX();
                yPos = getY();
                yPos -= 1;
                getWorld().addObject(new trail(), xPos, yPos);
                break;
            case 180:
                xPos = getX();
                yPos = getY();
                xPos += 1;
                getWorld().addObject(new trail(), xPos, yPos);
                break;
            case 270:
                xPos = getX();
                yPos = getY();
                xPos += 1;
                getWorld().addObject(new trail(), xPos, yPos);
                break;
        }
        if (Greenfoot.isKeyDown("left"))
        {
       setRotation(180);
        }
        
        if (Greenfoot.isKeyDown("right"))
        {
       setRotation(0);
        }
        
        if (Greenfoot.isKeyDown("up"))
        {
        setRotation(270);
        }
        
        if (Greenfoot.isKeyDown("down"))
        {
        setRotation(90);  
        }
        
        
  }
  
  private void checkDeath()
    {
        int Rotation = getRotation();
        switch(Rotation)
        {
            case 0:
                Actor object = getOneObjectAtOffset(getImage().getWidth()/2, 0, trail2.class);
                if (object != null)
                {
                    death = true;
                    redbike = true;
                }
                break;    
                
            case 90:
                object = getOneObjectAtOffset(0, getImage().getWidth()/2, trail2.class);
                if (object != null)
                {
                    death = true;
                    redbike = true;
                }
                break;
                
            case 180:
                object = getOneObjectAtOffset(-getImage().getWidth()/2, 0, trail2.class);
                if (object != null)
                {
                    death = true;
                    redbike = true;
                }
                break;
                      
            case 270:
                object = getOneObjectAtOffset(0, -getImage().getHeight()/2, trail2.class);
                if (object != null)
                {
                    death = true;
                    redbike = true;
                }
                break;
                
        }
    
        if (getOneIntersectingObject(trail2.class) !=null)
        {
            death = true;
            redbike = true;
        }     
        
        if (getWorld() instanceof Arena)
        {
        if (death == true)
        {
        Greenfoot.setWorld(new ArenaTwo());
        }
      }
    } 
danpost danpost

2018/11/29

#
Take a look at your checkDeath method. You got through all rotation to do pretty much the same thing as the following if with getOneIntersectingObject -- quite redundant. Remove lines 59 to 98. Next, notice that you ask about death right after it could possibly be changed. Better coding would be to just do what needs done when it is changed (as by copy/pasting lines 106 thru 111 to be inserted at line 103. In fact, that way, you would not need either variable death or redbike/bluebike.
public void checkDeath()
{
    if (getOneIntersectingObject(trail2.class) != null)
    {
        if (getWorld() instanceof Arena) Greenfoot.setWorld(new ArenaTwo()) else Greenfoot.stop();
    }
}
You would still need to add death by bike collision (maybe just adding another condition along with 'getOneIntersectingObject(trail2.class) != null'.
You need to login to post a reply.
1
2
3