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

2019/2/12

Variable from MyWorld

JPEG JPEG

2019/2/12

#
Hi there! So I have a variable called "Random" in my world and I need to use it in an actor called "Paper". The purpose of the Random variable that it is a case switch-which ever case it follows, will have a new actor assigned to it. On the actor Paper, I need to use Random in an if statment: I'm a prefer python so I'm not sure how to code this. In python it will look something like: if Random == Rock: While running something similar on Greenfoot, it states "cannot find variable random". How would I use a variable from MyWorld onto an actor?
danpost danpost

2019/2/12

#
You are going about it all wrong. This comparison should be done in your MyWorld class. Your actors have no real actions -- except to be clicked on, which the world can check for. All main game controls should be done in your game world's act method. detect click on actor make random computer choice determine results show results finalize/reinitialize ... repeat
JPEG JPEG

2019/2/12

#
Thank you! That makes things alot easier, however, I have yet hit another road block. I'm not sure how, but I treat my actors like Variables. How can I overcome this? For example, I have an actor called Paper. In the IF statment, I wrote, "if (random =! Paper)". Random works because it is a varaible. How can I write a statment to ask whether Random (which is a variable for a switch case method)....it may be easier if I show the code: random = Greenfoot.getRandomNumber(3); switch (random) { case 0: addObject(new Rock(), getWidth()/2,200);//return new Rock(); case 1: addObject(new Paper(), getWidth()/2,200);//Stapling the actors into place case 2: addObject(new Scissors(), getWidth()/2,200);//return new Scissors(); } } public void act() { if (Greenfoot.mouseClicked(null)) { Actor actor = Greenfoot.getMouseInfo().getActor(); if (actor instanceof Paper);{ if (random != Scissors); } I guess another way to word this, is how do I figure out what case random has been assigned to? e.g If random followed case 2 (as I already said that the actor paper has to be pressed), the IF statement will mean that scissor's actor will be removed.
danpost danpost

2019/2/13

#
JPEG wrote...
I'm not sure how, but I treat my actors like Variables. ... how do I figure out what case random has been assigned to? e.g If random followed case 2 (as I already said that the actor paper has to be pressed), the IF statement will mean that scissor's actor will be removed.
Place your actors in an array field:
Actor[] choices = { new Rock(), new Paper(), new Scissors() };
They will each now have a number associated with them:
0 for "rock" (choices[0])
1 for "paper" (choices[1])
2 for "scissors" (choices[2])
Your act method can then start with:
int playerChoice;
for (playerChoice=0; playerChoice<3; playerChoice++) if (Greenfoot.mouseClicked(choices[playerChoice])) break;
if (playerChoice == 3) return;
// continuation (choice made -- make comp choice, compare and show results)
It is easy enough to compare the numbers representing the choices made.
You need to login to post a reply.