PDA

View Full Version : Creating a spinner

john316
05-04-2015, 07:52 PM
The spinner object is already in Alice. How do I make a character move that many spaces for which ever number the arrow lands on (using random number). I tried using object variables and and setting it to each number on the spinner but it doesn't work. I'm completely clueless.

RavenOfCode
05-05-2015, 03:42 PM
Is this for school? Just wana make sure I can post coding changes... :)

john316
05-05-2015, 04:10 PM
It's for a project, I'm completed the rest of the project , but this is the only issue I'm having

RavenOfCode
05-05-2015, 06:22 PM
Ok, thanks for letting me know that.

From what I understand you are turning the spinner then checking the number, try it the other way around.
Start by calculating the number first, then turn the spinner according to the number calculated.

Hope this helps for you out.

Best,
RavenOfCode

john316
05-06-2015, 10:54 AM
That's the problem I'm completely clueless on how to do that. There is no ''When'' method for example, when arrow pointer is at or = to object parameter , move this many spaces. I'll still try to figure it out.

RavenOfCode
05-06-2015, 08:49 PM
That's the problem I'm completely clueless on how to do that. There is no ''When'' method for example, when arrow pointer is at or = to object parameter , move this many spaces. I'll still try to figure it out.

Use something like:

SpinnerV (a number variable) set value to random number (how every many spinner sections there are)

Spinner turn left .25 (or whatever number works) * spinnerV

This is a basic outline for what to do... all the exact amounts ill let you figure out.

chickentree
05-08-2015, 06:35 PM
The spinner object is already in Alice. How do I make a character move that many spaces for which ever number the arrow lands on (using random number). I tried using object variables and and setting it to each number on the spinner but it doesn't work. I'm completely clueless.

There are two ways I've found that will do what you want. By far the easiest is using poses the work is all "up front" i.e. done in the design not during execution but it works.

Select the spinner and in the properties panel (Lower left of Alice) click on "Create Pose" use "initialPose" for the name.
Repeat the step above but use "onePose" for the name (I am assuming the spinner is initially pointing at one.)
I'm assuming you are using the spinner with 20 divisions. So movement between the segments is 1/20th of a rotation, or 0.05 revolutions.
In the Alice IDE right click on the spinner's Arrow ( the whole arrow not just the head.) Then select Methods-Turn-Left and enter 0.05 for the amount.
Now select the whole spinner.
In the "Properties" tab at the left, click the "Capture Pose" button and give the pose a name. I used the name of the segment and added "Pose" on the end. As in "twoPose", this will help you if you end up using the section name in your program.
Repeat steps 3 through 6 until you have a pose for each spinner position.

To simplify things further you can create an array variable containing the poses but remember the array indexes start at 0 so if you wanted the spinner to go to 6 you would use an index of 5, or more generally index = spinValue-1.

The only problem I have found with this approach is that in going from one pose to another the spinner will take the shortest path, sometimes turing right and other times turning left. To get around this you could identify cases where the spinner would go the wrong way and take two steps to get to the desired value. The first would be a segment between the initial and final values in the direction you want the Arrow to turn. This would ensure the Arrow travels in the right direction.

Mark

john316
05-09-2015, 08:47 PM
I tried doing the poses, but the player isn't moving the respective places. Attached is what I have done so far.

There are two ways I've found that will do what you want. By far the easiest is using poses the work is all "up front" i.e. done in the design not during execution but it works.

Select the spinner and in the properties panel (Lower left of Alice) click on "Create Pose" use "initialPose" for the name.
Repeat the step above but use "onePose" for the name (I am assuming the spinner is initially pointing at one.)
I'm assuming you are using the spinner with 20 divisions. So movement between the segments is 1/20th of a rotation, or 0.05 revolutions.
In the Alice IDE right click on the spinner's Arrow ( the whole arrow not just the head.) Then select Methods-Turn-Left and enter 0.05 for the amount.
Now select the whole spinner.
In the "Properties" tab at the left, click the "Capture Pose" button and give the pose a name. I used the name of the segment and added "Pose" on the end. As in "twoPose", this will help you if you end up using the section name in your program.
Repeat steps 3 through 6 until you have a pose for each spinner position.

To simplify things further you can create an array variable containing the poses but remember the array indexes start at 0 so if you wanted the spinner to go to 6 you would use an index of 5, or more generally index = spinValue-1.

The only problem I have found with this approach is that in going from one pose to another the spinner will take the shortest path, sometimes turing right and other times turning left. To get around this you could identify cases where the spinner would go the wrong way and take two steps to get to the desired value. The first would be a segment between the initial and final values in the direction you want the Arrow to turn. This would ensure the Arrow travels in the right direction.

Mark

RavenOfCode
05-10-2015, 09:33 AM
My way works...

This took me about 3 mins to make...

Calculate the spinner variable first then turn it accordingly...

chickentree
05-10-2015, 04:10 PM
My way works...

This took me about 3 mins to make...

Calculate the spinner variable first then turn it accordingly...

Yep, it is better than either of mine. I hadn't thought of using just one initial position but it is a lot nicer solution. But the problem with the test.a2w John316 submitted shows a couple of other problems.
John316, you should understand that making the spinner spin is just appearance, smoke and mirrors, eye candy for the program. It is a way to show the user what the result of the spin was. Alice seems to let you compare poses but it doesn't work correctly and even if it did you are comparing two poses without regard to where the arrow is actually pointing. Your "if" statements will do the "if" part every time regardless of the pointers location.
As RavenOfCode said in a previous post, you want to use a variable to hold the value returned from "random number" (Raven used world.spins in the code he posted.) You then use the value of world.spins to both position the arrow on the spinner and to move the character forward.
One more thing, the "Boy" you are using has a walk method. If you call Boy.walk(world.spins) the boy should walk that number of steps.

Mark

MrMoke
05-10-2015, 06:23 PM
Just remember that poses are not always an efficient solution to a programming algorithm, and that there are many other viable solutions. The attached proggie uses only a single array, and the positions of the objects in the array to generate a random number of steps.

I left a key item visible so that you could see what's going on, but it can easily be made invisible. There is also no need for a function because the stepcount variable is global.

The two code blocks are disabled, so you can enable the first to view it, then disable it and enable the second to run the test.

Collisions are sometimes good for you :eek:

john316
05-10-2015, 06:55 PM
Thanks for the help. Thank you for being so patient. 1 last question. Because the boy will be on a game board he needs to move each square. To land in the middle of each square he must move .31 meters. How do I set it so so that he will that distance( .31 meters) ''x'' amount of times (whatever number the spinner lands on) ?

Thanks again.

MrMoke
05-11-2015, 09:06 AM
The number that's passed back is the multiplier.

RavenOfCode
05-11-2015, 06:21 PM
The number that's passed back is the multiplier.

ie: .31 * spins (or whatever variable you are using).

Also Mr. Moke your example is really a creative idea... I like your use of arrays...