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

2014/6/2

Randomly select one of my actors randomly when the world it clicked on

FootLongTurkey FootLongTurkey

2014/6/2

#
I am attempting to create a programme which will randomly select 1 of 90 actors and change their transparency from the '0' which I have set, to '255' which is the transparency I want. I have tried many different techniques all of which have sadly failed so I need help from the discussions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
 
    public void act()
    {
 getImage().setTransparency(0);
}
 
  public int isKeyDown1;
   {
if (Greenfoot.getRandomNumber(91) == 1)
{
getImage().setTransparency(255);
}
}
}
davmac davmac

2014/6/2

#
My eyes are bleeding :( please fix your indentation! :p is 'isKeyDown1' supposed to be a variable or a method? You have some code which isn't inside any method currently. Maybe that's the problem.
FootLongTurkey FootLongTurkey

2014/6/3

#
I know my code is shocking, im new so im still trying to understand these things. I'm currently trying to create new code but if I were to do so how would I go about making it. I just need a point of reference to go off of
davmac davmac

2014/6/3

#
I know my code is shocking
The code itself is not so bad, but you haven't indented properly. Considering how easy it is to do that (press ctrl+shift+I...) there's no excuse... Also, if you answer the questions I asked, maybe I can help further.
PseudoScrub PseudoScrub

2014/6/3

#
Sorry new account because I spammed my friends discussion. isKeyDown is meant to be a variable. The same code is currently attached to each of my actors, each having a minor difference. In order for it to go through the process of selecting a random number when the world is clicked do I have to directly click that specific actor, or is there another method where I only have to click the environment which will involve all actors?....Sorry for the long paragraphs :(
davmac davmac

2014/6/4

#
Ok, if you agree not to spam useless messages again then I will re-enable your account.
In order for it to go through the process of selecting a random number when the world is clicked do I have to directly click that specific actor, or is there another method where I only have to click the environment which will involve all actors?....Sorry for the long paragraphs :(
So you want to select a random actor when the world is clicked? In that case, I would have the code in your world subclass. You could do something like this:
1
2
3
4
5
if (Greenfoot.mouseClicked(null)) {
    List all = getObjects(MyActor.class);
    MyActor a = (MyActor) all.get(Greenfoot.getRandomNumber(all.size());
    a.setTransparency(255);
}
You would put this in the world's act method.
FootLongTurkey FootLongTurkey

2014/6/4

#
Thank you, I'll try this code ASAP. Would I have to make any adjustments to my Actors? Or can I delete the code apart from the Transparency(0) and let this code take control?
davmac davmac

2014/6/4

#
Would I have to make any adjustments to my Actors?
If you leave the setTransparency(0) call in the act() method of the actors, then their transparency will be set back to 0 every time they act. That's probably not what you want. Add a constructor and set it to 0 there, instead:
1
2
3
4
MyActor()
{
    getImage().setTransparency(0);
}
You should remove all other code from your Actor subclass. Also, my code above was slightly wrong, corrected version:
1
2
3
4
5
if (Greenfoot.mouseClicked(null)) { 
    List all = getObjects(MyActor.class); 
    MyActor a = (MyActor) all.get(Greenfoot.getRandomNumber(all.size()); 
    a.getImage().setTransparency(255); 
}
FootLongTurkey FootLongTurkey

2014/6/4

#
Would I place it in the provided 'public Background()'?
davmac davmac

2014/6/4

#
Would I place it in the provided 'public Background()'?
Which part? That sounds like a world constructor. Neither part goes in the world constructor. You want to set the transparency to 0 in the actor constructor, and you want to check for mouse click etc in the world's act method (which you will need to create). Anything that you want to happen continuously needs to go in an act() method. Anything that only needs to happen when an object is created can go in a constructor.
FootLongTurkey FootLongTurkey

2014/6/5

#
A third ')' is expected after the all.size()
You need to login to post a reply.