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

2014/9/11

How do I get a counter to count at double the speed if I hit an actor

Ben&Sam Ben&Sam

2014/9/11

#
I can't figure out how to do this I have an actor called Bee, and I want the counter to count double if the actor Bee hits the Actor balloon, I have tried coding it within the world but you can't define getoneobjectatoffset in the world and, so I need to program it within the code of the bee here is my code so far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bee here. * * @author (your name) * @version (a version number or a date) */ public class Bee extends Actor { private Counter actCounter; /** * Act - do whatever the Bee wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.isKeyDown("down")) { setRotation(90); move(1); setRotation(0); } if(Greenfoot.isKeyDown("Up")) { setRotation(270); move(1); setRotation(0); } Actor Bomb = getOneObjectAtOffset(0,0, Bomb.class); if (Bomb!=null) { Greenfoot.setWorld(new TheWorld()); System.out.println("Game Over"); } Actor Balloon = getOneObjectAtOffset(0,0, Balloon.class); if (Balloon!=null) { actCounter.setValue(actCounter.getValue() + 1); } } }
Ben&Sam Ben&Sam

2014/9/11

#
sorry meant actCounter.setValue(actCounter.getValue() + 2);
Super_Hippo Super_Hippo

2014/9/11

#
Use the 'code' tags next time please! You need a reference to the counter that you (probably) create in your world class. So to create the Counter, you have something like the following in the world class:
1
2
3
4
5
//outside methods
public static Counter actCounter;
//when creating
actCounter = new Counter();
addObject(actCounter, /*x*/, /*y*/);
Then you can just use
1
worldName.actCounter.add(2)
If you have the imported Counter which comes with the 'add' method. If you have more than one Counter, you should not have the reference as 'static'. Instead, you can access it through this:
1
2
worldName w = (worldName) getWorld();
w.actCounter.add(2);
Or better, you save the world in your class, so you don't have to get the world all the time. (Either with the 'addedToWorld' method or as a parameter in the constructor.)
Ben&Sam Ben&Sam

2014/9/11

#
Cheers, it works
You need to login to post a reply.