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

2012/5/19

Having trouble with get one object at offset

Scoter_man1 Scoter_man1

2012/5/19

#
Everything compiles, its just that my code doesnt actually DO anything. I've put in system.out.println's so I can see if the code is making it to certain checkpoints. The main character has this code inside his attack method. Moblin is the monster he is attacking. So I've tried attacking only from the right so far (which I would think this code would work for). moblin2 is always null though. It never makes it inside the if statement. Can anyone help me get this so it doesnt return null? Thanks
Moblin moblin2 = (Moblin)getOneObjectAtOffset((getX()+150),getY(),Moblin.class);
            if (moblin2!=null)
            {
                moblin2.setHealth(-1);
            }
danpost danpost

2012/5/19

#
Drop the 'getX()' and the 'getY()' in the parameters of the 'getOneObjectAtOffset', making the value 'offsets', instead of 'locations' (hence the name of the method). You probably want something like this in line 1:
Moblin moblin2 = (Moblin) getOneObjectAtOffset(getWidth() / 2 + 1, 0);
Scoter_man1 Scoter_man1

2012/5/19

#
OH, duh, that totally makes sense. Thank you very much danpost.
You need to login to post a reply.