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

2013/1/18

problems with my code again

theuberjew theuberjew

2013/1/18

#
my world edge wasn't set right so I made some big changes to it and now i'm broken and confused. import greenfoot.*; import java.awt.Color; import java.util.List; public class enemy extends Actor { private int size=5; // move this up here public enemy() // had a semicolon here { size=Greenfoot.getRandomNumber(274); } public void act() { // enemy(); - you can't call your constructor move(5); lookForRocket(); if (enemyHit = true) { //whatever should happen if the game is over; death(); } destroy(); } public void death() { if (getX()==599) { getWorld().removeObject(this); } } public boolean canSee(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); return actor != null; } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } // forgot this bracket public void move() { setLocation(getX()+2,getY()); } public void lookForRocket() { if ( canSee(Rocket.class) ) { eat(Rocket.class); } } public void destroy() { if (getX()==599) { getWorld().removeObject(this); } public boolean enemyHit() { return getOneIntersectingObject(enemy.class) != null; } //public boolean enemyHit() { // List<enemy> enemiesInWorld = getWorld().getObjects(enemy.class);//Enemy has to be the classname; If this is not the right classname you have to change it; // for (enemy enemy : enemiesInWorld) { //if (intersects(enemy)) { //return true; } // } // return false; //} //}
danpost danpost

2013/1/18

#
This statement: if (enemyHit = true) should be giving you an error message saying something like 'cannot find variable enemyHit'. To make it call the method, use enemyHit(). At this point, you will probably get another message because you cannot assign a value to a method. This is a result of using an assignment operator '=' instead of the conditional equality operator '=='. A note on this: being the method 'enemyHit()' returns a boolean value, it is not neccessary to compare it the 'true'. if(enemyHit()) does the same job. I did not look further into the code, so if you are still getting error messages, post the message with the code; and use the code tag below the 'Post a reply' input box to place your code into (if you cannot get the code tag to work, put the tags in manually; before your code and after it. One more thing, before copying your code press Ctrl-Shift-I to properly indent the code.
You need to login to post a reply.