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

2023/12/14

Help identifying another objects image

Player2IsHere Player2IsHere

2023/12/14

#
Hi, currently I am a novice, coding a basic video game. The premise is a football game where the user will pass the football over the defender to the receiver, where the user will then control the movements of the wide receiver to try and score a touchdown. My issue right now, is creating the tackle() method for the defender. The purpose of the method is to try to tackle the receiver once he has the ball. However my issue is that the defender can only attempt to tackle the wide receiver once he has the ball. The check method im trying to use is checking to see if the wide receiver has set his image to "RecieverWithBall". I am currently using the following code to retrieve the objects code and then try to compare it with whether that image is "RecieverWithBall.png" or not. But it just will not work.
if(getWorld().getObjects().get(0).getImage() == new GreenfootImage("RecieverWithBall.png"){
//some code to chase the reciever
}
danpost danpost

2023/12/14

#
Player2IsHere wrote...
I am currently using the following code to retrieve the objects code and then try to compare it with whether that image is "RecieverWithBall.png" or not.
If you think that: << some already created image >> will be equal to << some image created on the spot >> then you are mistaken. This type of "equals" requires the same instance of an object on both sides of the equation to be true. Maybe you can check to see if the toString method returns a String object containing the filename:
if (getWorld().getObjects().get(0).getImage().toString().contains(" RecieverWithBall.png ")) {
or, to be really sure:
if (getWorld().getObjects().get(0).getImage().toString().contains("Image file name: RecieverWithBall.png")) {
Player2IsHere Player2IsHere

2023/12/14

#
Thanks!
You need to login to post a reply.