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

2016/4/18

Hit method assitance

StoveAteMe StoveAteMe

2016/4/18

#
Okay so, what my intention is for this piece of code is, for when a rocket object is touching the savior object, I want the rocket object to disappear and for some health to be reduced from the savior. But the problem is, whenever I get those two objects to touch, they simply pass through each other. I think it's an issue with my if statement but I'm not quite sure what is wrong with it.
public void Hit()
  {
    Actor rocket; 
    rocket = getOneObjectAtOffset(0, 0, Rocket.class);
      if(savior != null) {
          getWorld().removeObject(rocket);
          health = health - 25;
        }
    
    
  }
danpost danpost

2016/4/18

#
Line 5 look a bit suspicious. What is 'savior' referring to and why would it possibly be 'null'? (maybe that should be 'rocket'?)
StoveAteMe StoveAteMe

2016/4/19

#
Alright so, scratch the previous piece of code and look at this one please.
  public void Hit()
  {
    Actor rocket; 
    rocket = getOneObjectAtOffset(0, 0, Rocket.class);
      if(isTouching(rocket)) {
          getWorld().removeObject(rocket);
          health = health - 25;
        }
    
    
  }
The error is at my if statement, and located on "(rocket)" And it says, "incompatible types: greenfoot.Actor can not be converted to java.lang.Class<?> For starters, what exactly does this error mean, and how can I work around it.
danpost danpost

2016/4/19

#
StoveAteMe wrote...
The error is at my if statement, and located on "(rocket)" And it says, "incompatible types: greenfoot.Actor can not be converted to java.lang.Class<?> For starters, what exactly does this error mean, and how can I work around it.
It means that the 'isTouching' method requires a parameter of type class, not of type Actor (which you are trying to give it). Did you try just replacing 'savior' in your first code post with 'rocket'?
StoveAteMe StoveAteMe

2016/4/19

#
Yeah I did try replacing 'savior' with 'rocket', and it did not change anything. (If you're wondering I tried a different approach to solving the if statement after your suggestion didn't work, probably should have communicated that earlier). And I did try, for my if statement, inputting 'Rocket.class' but that did not do anything to solve the problem.
danpost danpost

2016/4/19

#
StoveAteMe wrote...
Yeah I did try replacing 'savior' with 'rocket', and it did not change anything. (If you're wondering I tried a different approach to solving the if statement after your suggestion didn't work, probably should have communicated that earlier). And I did try, for my if statement, inputting 'Rocket.class' but that did not do anything to solve the problem.
It must be one of three things. Either (1) you did not do what I said like I said, (2) you are not calling the 'Hit' method from the 'act' method or a method it calls or (3) you placed a conditional (used an if) that regulates the call to the 'Hit' method -- all presuming that the code given is located in the class of the savior.
StoveAteMe StoveAteMe

2016/4/19

#
Oh okay, the issue was your second suggestion, it's working as I want now. Thanks for your help!
You need to login to post a reply.