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

2017/12/4

Trying to add score when actor gets to 100 on the x axis

ryan22 ryan22

2017/12/4

#
In my project, I'm trying to add 1 to the score when my main actor passes another actor. Can someone help please?
counterBart++;
           if(counterBart == 40){
               
               score1.setScore(score);
               Bart b = new Bart();
               if (b.getX() == 100){
                   score++;
                }
               int y = rand.nextInt(350)+50;//Creates random placement of Burns on Y-axis with 50 pixels on top and bottom of area of space
               GreenfootImage bimage = b.getImage();
               addObject(b, getWidth(), y);
               System.out.println(counterBart);
               counterBart = 0;
            }
Super_Hippo Super_Hippo

2017/12/4

#
I have no clue what the Bart class is in this context, but maybe you are seeing your mistake: Line 5: You create a new Bart object Line 6: You are trying to get the X coordinate of the new Bart object which isn't even in the world
ryan22 ryan22

2017/12/4

#
public class Bart
{
    int speed = 7;
    
    public void act() 
    {
        setLocation(getX() - speed, getY() );
    }    
}
This code make the Bart class move across the screen at a -7 pixel rate. I was trying to say at the line x=100 that the counter of 'score' would increase by one.
Super_Hippo Super_Hippo

2017/12/4

#
The Bart object will only move across the screen when its act method is executed. The act method is only executed when it is in the active world. In the code above, you add it in line 11. Why doesn't the Bart class extend the Actor class? You can either save a reference to your objects in your world and check their locations later or these objects do it in their act methods.
ryan22 ryan22

2017/12/5

#
I don't think I understand what you are getting at...
danpost danpost

2017/12/5

#
The Bart class needs to be a subclass of Actor. A Bart object needs to be added into the world before you can get a coordinate value from it. A Bart object can check its own location after moving to see if has an x-coordinate value of 100. A Bart object must not skip over the x-coordinate of 100 if the score is to be adjusted (meaning it must be placed in the world at some (100+7*n) x-coordinate value where n is some positive integer). Please show your entire world class code so that the location of the snippet within the class can be ascertained.
ryan22 ryan22

2017/12/5

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
import java.lang.*;


public class MyWorld extends World
{
    private int counterFlanders = 0;
    private int counterBurns = 0;
    private int counterBart = 0;
    private int counterPS = 0;
    private int levelcounter = 0;
    private int DonutTime = 0;
    public int score = 0;
    public int lives = 1;
    private int plusminus = 0;
    public int randomposition;
    Score score1 = null;
    public GreenfootSound themesong;
    Random rand = new Random();
    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        super(525, 400, 1, false); 
        themesong = new GreenfootSound("fulltheme.wav");
        //Create Homer Obj
        HomerJSimpson Homer = new HomerJSimpson();
        Lives lives = new Lives();
        score1 = new Score();
        
        //Add Homer to World
        addObject(Homer, 100, getHeight()/2);
        addObject(lives, 60, 20);
        addObject(score1, 465, 20);
        
        setPaintOrder(Score.class, Lives.class, GameOver.class, HomerJSimpson.class, Donut.class, Bart.class, Burns.class, PandS.class, Flanders.class);
        }
        public void started(){
        themesong.playLoop();
    }
    public void act(){
        levelcounter++;
        //plusminus ++;
        DonutTime++;
        if(levelcounter < 1000){
        counterFlanders++;

        if(counterFlanders == 60){
            score++;
            score1.setScore(score);
            Flanders flanders = new Flanders();
            /*if (flanders.getX() == 100){
                   score++;
                }*/
            int y = rand.nextInt(350)+50; //Creates random placement of Flanders on Y-axis with 50 pixels on top and bottom of area of space
            GreenfootImage fimage = flanders.getImage();
            addObject(flanders, getWidth(), y);
            System.out.println(counterFlanders);
            counterFlanders = 0;
        }
       }
       if(levelcounter>= 1000 && levelcounter < 2000)
       {
           counterBurns++;
           if(counterBurns == 50){
               score++;
               score1.setScore(score);
               Burns monty = new Burns();
               /*if (monty.getX() == 100){
                   score++;
                }*/
               int y = rand.nextInt(350)+50;//Creates random placement of Burns on Y-axis with 50 pixels on top and bottom of area of space
               GreenfootImage burnsimage = monty.getImage();
               addObject(monty, getWidth(), y);
               System.out.println(counterBurns);
               counterBurns = 0;
            }
        }
        if(levelcounter>= 2000 && levelcounter < 3000)
       {
           counterBart++;
           if(counterBart == 40){
               score++;
               score1.setScore(score);
               Bart b = new Bart();
               /*if (b.getX() == 100){
                   score++;
                }*/
               int y = rand.nextInt(350)+50;//Creates random placement of Burns on Y-axis with 50 pixels on top and bottom of area of space
               GreenfootImage bimage = b.getImage();
               addObject(b, getWidth(), y);
               System.out.println(counterBart);
               counterBart = 0;
            }
        }
        if(levelcounter>= 3000 && levelcounter < 4000)
       {
           counterPS++;
           if(counterPS == 30){
               score++;
               score1.setScore(score);
               PandS ps = new PandS();
               int y = rand.nextInt(350)+50;//Creates random placement of Burns on Y-axis with 50 pixels on top and bottom of area of space
               /*if (ps.getX() == 100){
                   score++;
                }*/
               GreenfootImage psimage = ps.getImage();
               addObject(ps, getWidth(), y);
               System.out.println(counterPS);
               counterPS = 0;
            }
        }
       if (DonutTime == 400){
            Donut mmm = new Donut();
            randomposition = rand.nextInt(325)+1;
            GreenfootImage mmmimage = mmm.getImage();
            addObject(mmm, getWidth(), randomposition);
            System.out.println(DonutTime);
            DonutTime = 0;
        }
       if(levelcounter < 1000){
            setBackground("MyWorld.png");       
        }
       if(levelcounter == 1000){
            setBackground("LevelTwo.png");
            System.out.println(levelcounter);
        }
       if (levelcounter == 2000){
            setBackground("Lvl4.png");
            System.out.println(levelcounter);
        }
       if(levelcounter == 3000){
            setBackground("Lvl3.png");
            System.out.println(levelcounter);
        }
       if(levelcounter == 4000){
            levelcounter = 0;
        }
       if(plusminus == 750){
           int coin = rand.nextInt(2)+1; //picks random number: 1 or 2 (like flipping a coin)
           if(coin == 1){
               PlusTen pt = new PlusTen();
               int position = rand.nextInt(325)+1;
               GreenfootImage ptImage = pt.getImage();
               addObject(pt, getWidth(), position);
               plusminus = 0;
            }
            else{
               MinusTen mt = new MinusTen();
               int position = rand.nextInt(325)+1;
               GreenfootImage mtImage = mt.getImage();
               addObject(mt, getWidth(), position);
               plusminus = 0;
            }
        }
    }
}
The original question just applies to the if statements with score++.
danpost danpost

2017/12/5

#
Okay, again, it would be better to have the actors that move left across the screen check their x-coordinate value and add to the score when needed:
public void act()
{
    move(-speed);
    if (getX() <= 100 && getX()>100-speed)
    {
        MyWorld mw = (MyWorld)getWorld();
        mw.score++;
        mw.score1.setScore(mw.score);
    }
}
ryan22 ryan22

2017/12/5

#
What class would this be in? MyWorld or Bart?
ryan22 ryan22

2017/12/5

#
Nvm
ryan22 ryan22

2017/12/5

#
Thanks sm
You need to login to post a reply.