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

2014/3/14

counter help

1
2
danpost danpost

2014/3/15

#
Ok. You can do this (since you are stopping the world): (1) get a reference to the Counter object just line you did in the Bullet class in the 'bumpCounter' method (lines 28 and 29); (2) change the location and image of the counter with code something like this:
GreenfootImage image = new GreenfootImage("FINAL SCORE\n"+counter.getValue(), 84, java.awt.Color.green, null);
counter.setImage(image);
counter.setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);
hugtvy hugtvy

2014/3/15

#
sorry, im an idiot so i dont quite know what you mean by get a reference to the counter. where do i put the code youve given me?
danpost danpost

2014/3/15

#
Put it in the location where you determine game over. And I stated where you can find how to get the reference (see (1) above).
hugtvy hugtvy

2014/3/15

#
ok cheers what does get a reference mean also the code you gave me, the getValue on line 1 doesnt work.
danpost danpost

2014/3/15

#
'get a reference' means to declare a local variable of the type required (in this case, of Counter type) and assign the type object to it. You have one Counter object in your world that you need to access to adjust or get its score. But you do not have any fields (fields are declared outside of methods within a class) holding a reference to that object in your world. 'Counter counter;' (within a method) declares a local variable of type Counter called counter to hold a Counter object. 'counter = getWorld().getObjects(Counter.class).get(0);' will assign to the counter variable the first Counter object retrieved in the list of Counter objects in your world. These two statements allows the variable counter to become a reference to that Counter within your world. You should now be able to use Counter methods on the reference: 'counter.add(5);' or 'int score = counter.getValue();' If 'getValue' does not work, it is because your reference is not assigned properly. Show the code you now have starting with the first line in the trouble area that has anything to do with the Counter object.
hugtvy hugtvy

2014/3/15

#
So where do i actually put this? haha sorry im really confused "Counter counter; counter = getWorld().getObject(Counter.class).get(0)"
danpost danpost

2014/3/15

#
hugtvy wrote...
So where do i actually put this? haha sorry im really confused "Counter counter; counter = getWorld().getObject(Counter.class).get(0)"
Before the three lines above.
hugtvy hugtvy

2014/3/15

#
alright, ive put this into my survivor class is that correct, this is where the game stops from? im guessing not since im getting an error about the "counter = getWorld().getObject(Counter.class).get(0);" ive tried this reference instead, its the one found in my bullet: counter = (Counter)world.getObjects(Counter.class).get(0); it does work with that reference code except i then get an error that stops the scenerio sayin there is a problem with this line: counter.setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2); cant i just set it coords instead of getWidth and getHeight?
danpost danpost

2014/3/15

#
Change the line you are having problems with to:
counter.setLocation(world.getWidth()/2, world.getHeight()/2);
hugtvy hugtvy

2014/3/15

#
that worked, haha thank you soo much, youve helped my game come a long way! dont think i have any other issues but if I do should i contact you on here or make a new discussion?
danpost danpost

2014/3/15

#
hugtvy wrote...
that worked, haha thank you soo much, youve helped my game come a long way! dont think i have any other issues but if I do should i contact you on here or make a new discussion?
If this issue is resolved, then another discussion thread would be more appropriate for other issues.
You need to login to post a reply.
1
2