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

2013/8/4

GetOneObjectAtOffset Help...

1
2
TippierNba TippierNba

2013/8/4

#
Hi this is my first post here, and i am trying to make an object change its color when another object(The Main Actor) is close to it using get getOneObjectAtOffset. This is the code i got so far. Img1 is the Original Image and Img is the one i want it to turn into. Actor Thing = getOneObjectAtOffset(getImage().getWidth()/2,getImage().getHeight()/2, Main.class); if(Thing==null) { setImage(img1); return false; }else{ setImage(img); return true; } Thanks!
If you just want to know if it's nearby, use getObjectsInRange(/*radius of range*/, Main.class). And see if the List returned has a size of 1 (or is not equal to zero, if you have more), instead if the one object returned is null or not. Use getOneObjectAtOffset when wanting to find if an object has intersected exactly on the spot from the actor's x and y coordinates as specified. For example, your current code will look at the pixel (or cell) directly below, and to the right of your actor. getOneObjectAtOffset(0,0,class) would look for the object to be intersecting the coordinates of the actor. getOneObjectAtOffset(1,1,class) would look to the right and down by 1 pixel/cell, getOneObjectAtOffset(-1,-1) would look up and left by 1 pixel/cell. Hoped that helped.
For some more clarification with getObjectsAtOffset or getOneObjectAtOffset, For x if negative, will look above if positive, will look below For y if negative, will look to the right if positive, will look to the left
TippierNba TippierNba

2013/8/4

#
Okay Thanks, How do u suggest I use getObjectsInRange? Thanks
Sorry, I should have clarified that better. Basically, getObjectsInRange returns a List of all the objects of a specific class inside the defined radius. If nothing of that class is there, it's size will be zero, if there is a least one of that object, the size won't be. So I would use the following code if you want to do this.
boolean mainNearby = getObjectsInRange(/*How close you want to the actor to be*/, Main.class).size() > 0;

if (mainNearby)
...
This will find a Main actor within how close you define it in the first parameter. Then you can continue with your if statement, but use the boolean variable instead of what you had before. Your code should now work if the Main actor is close to the color-changing object, from all directions, instead of just one.
TippierNba TippierNba

2013/8/4

#
Okay so I gave it a shot and ended up with this. Thanks Again for your help but nothing is happening. boolean mainNearby = getObjectsInRange(200,Main.class).size() > 0; if(mainNearby) { setImage(img); }else{ setImage(img1); } Also this the game im trying to make http://www.pembinatrails.ca/fortrichmondcollegiate/compsci/greenfoot/walls/Walls-export/Walls.html
So based off what I've seen, the blocks change color when you bump into them, but now you want them to change color if he's nearby. What does you're img and img1 look like? Because if they are the same image, it will look like nothing has changed. Also, I know this sounds a bit stupid, but is you're Main class really the player? I can't think of anything else that might cause this, but I don't have a full look at the code either. Not that you should post all the code, the code affecting the color of the blocks might be helpful.
TippierNba TippierNba

2013/8/4

#
Yes main is my main character. I would like to change the color when they bump into it but I dont know how to change the color of another actor(Yellow Class) within my Main class. This is the code in my Yellow Class. public GreenfootImage img1 = new GreenfootImage(30,30); public GreenfootImage img = new GreenfootImage(30,30); public Yellow() { GreenfootImage img = new GreenfootImage(30,30); img.setColor(Color.YELLOW); img.fill(); setImage(img); GreenfootImage img1 = new GreenfootImage(30,30); img1.setColor(Color.BLACK); img.fill(); }
Here's you're problem: You created new instances of img1 and img inside the constructor of Yellow. Get rid of "GreenfootImage" behind the second occurrences of img and img1, then you will be modifying the variables that are visible to the whole class, instead of variables visible to the constructor. Then it should work (I think)
TippierNba TippierNba

2013/8/4

#
Thanks that did it, however it wasnt the result i was looking for. As you said before in the Game I am trying to mimic the Wall changes color when it bumps into it. This is the Code in my main class, How can i add the color change into this code? public boolean CheckLWall1() { int Width = getImage().getWidth(); int xDis = (int)(Width/2); Actor rightWall = getOneObjectAtOffset(-xDis,0,Green.class); Actor rightWall1 = getOneObjectAtOffset(-xDis,0,Red.class); Actor rightWall2 = getOneObjectAtOffset(-xDis,0,Blue.class); Actor rightWall3 = getOneObjectAtOffset(-xDis,0,Yellow.class); if(rightWall ==null) { return false; } else { setLocation(getX()+1,getY()); //When bumped into go back 1. return true; }
My guess is that Green is the only wall that will light up? The reason being is that you only check for rightWall, not for rightWall1, rightWall2, etc. You need to check for all of them. I think you should make an array or an ArrayList of Actors, then add each rightWall into it. If all of them equal null, then you return false, if one of them equals true, return true. If you don't know yet how to go through an Array or ArrayList, this code should help
Actor[] array = /*All the values*/

//First way
for (int i = 0; i < array.length; i++)
{
    if (array[i] != null)
    {
        setLocation(getX()+1,getY());  //When bumped into go back 1. 
       return true;
    }
}

return false;

//Second way
for (Actor a : array)
{
    if (a != null)
    {
       setLocation(getX()+1,getY());  //When bumped into go back 1. 
       return true;
    }
}

return false;
Keep in mind that it will never go to "return false" UNLESS all of the values in array = null. Return will stop the method immediately, and won't continue with the rest of the code.
TippierNba TippierNba

2013/8/4

#
I already have it checking for all of them. I just did not post the code. That is my fault sorry. What i want to happen is when it bumps into the wall Line 8. Happens and along with that i want the color to change.
Get rid of: "else{ setImage(img1); }" In the last color-changing method to be called in the act statement.
TippierNba TippierNba

2013/8/4

#
Okay this is my problem, I want my main class to change the Image of my Yellow Class. Is that possible? So basically is it possible to change the Image of the "Yellow Classs" from my "Main Class"? If so how?
Sorry for the later response, was eating some breakfast, and longer description. Yes, you can. If you make a method public in the Yellow class (the one you want to change the color) and then you find the object using getOneObjectAtOffset as before, you can do this:
rightWall3.changeColor() //Or whatever your method is called
Notice the dot, this is how you call methods in java outside of the class they were made in. Only if the method is declared public (take a look at the act method, it's "public void act()") When you want to change the color, as long as you have a reference to the object (rightWall3 is a reference) you can do it. However! If you do what I just told you without casting rightWall3 to Yellow, you will get an error. If you don't know how to cast, let me give you a quick explanation: Casting is basically changing how the computer views the object. With getOneObjectAtOffset(), it returns an actor, but we both know that it really is a Red, Green, Blue, or Yellow object. But, it is also an Actor, being a subclass, or child, of the Actor class. But we want to treat it like a Yellow object, instead of an Actor. If you try calling the color changing method, you will get an Error, because it is treated like an Actor, and the Actor class does not have your color changing method. So we need to tell the computer that it is a Yellow object, not just a Actor (because it is also an Actor). To do this, and call the color changing method, we do this:
Yellow yellowWall = (Yellow)rightWall3;
//Now that it's a Yellow object, we can treat it like one, instead of an actor
yellowWall.colorChangingMethod();
You need to be careful with casting. If you try to cast for example a Green object to Yellow, you will get an error. This is because Yellow is not the same, or a subclass of Green. If they were the same, or if Yellow was a subclass of Green it would work, but in this case, it doesn't. I hope that helps as well.
There are more replies on the next page.
1
2