Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   How to make an object no move a certain distance -A cont of my last Q (http://www.alice.org/community/showthread.php?t=6507)

 george3356 05-09-2011 11:48 PM

How to make an object no move a certain distance -A cont of my last Q

ASSIGNMENT: Make a rabbit chase a butterfly until it catches it w/ a net.
I am now trying to make sure my butterfly does not go to high or to low - so high the rabbit can't reach/crashing into the ground. I know I need an if statement here, but I'm just not sure what to do with it. Any thoughts?

(Again, I need to be sure the butterfly does not go to high or low using an IF/ELSE statement.)

 TauTrumpsPi 05-10-2011 06:59 AM

Just loop infinity times an if statement in a separate method that says if the butterfly goes too high, move down x meters.

 ieges 05-10-2011 08:25 AM

If butterfly distance away from ground is more than ____, then do _____-
If butterfly is within .1 meters of ground, do _____

Be sure to loop these both infinitely, otherwise they will only check at the beginning of the world.

I had this assignment also, so if you want, you can refer to it for help. The only thing I didn't do was loop the if statements infinitely, so remember to do that ;). (I think at least, but I could have missed something else)

Assignment: [url]http://www.dinkypage.com/113622[/url]

Oh, and also, here is what I think you need to use:

Boolean butterfly.rise ( )
No variables
If ( butterfly is within 0 meters of ground )
Return true
Else
Return false

Boolean butterfly.fall ( )
No variables
If ( butterfly is at least 1 meter away from ground )
Return true
Else
Return false

From there, you can say in your myfirstmethod that if _____ returns true, do a method that makes the butterfly go up or down.

 dar 07-12-2011 12:25 AM

okay I was able to get all this to work but how do i keep the net with the rabbit everytime he moves until it catches the butterfly

 ElectricKirby 07-12-2011 04:36 AM

Set the net's vehicle property to the rabbit. Preferable the hand he's holding it with.

 letsgojets 09-03-2013 10:51 AM

help

1 Attachment(s)
I tried to follow the suggestion and I keep getting an error when trying to run, can you please review and let me know what i might have missed

 DensetsuNoKaboom 09-03-2013 05:53 PM

When the method "butterfly.fly" is called, it tries to activate itself before moving, which causes it to activate itself, which causes it to activate itself, etc. until eventually Alice crashes. Remove the method "butterfly.fly" that's inside itself and that should fix it.

 letsgojets 09-03-2013 09:03 PM

help

1 Attachment(s)
thanks, that worked

so now my butterfly keeps going below ground and the rabbit cant catch it.

Any thoughts

 aliirfan01 10-10-2013 02:35 AM

what is it

[QUOTE=TauTrumpsPi;35504]Just loop infinity times an if statement in a separate method that says if the butterfly goes too high, move down x meters.[/QUOTE]

i aslo have plenty of common sense ...i can't answer...:cool:

 chickentree 10-11-2013 11:11 AM

This rant does answer your butterfly question.

[QUOTE=george3356;35496]ASSIGNMENT: Make a rabbit chase a butterfly until it catches it w/ a net.
I am now trying to make sure my butterfly does not go to high or to low - so high the rabbit can't reach/crashing into the ground. I know I need an if statement here, but I'm just not sure what to do with it. Any thoughts?

(Again, I need to be sure the butterfly does not go to high or low using an IF/ELSE statement.)[/QUOTE]

Do not use methods with infinite loops!
Why? Well, because it can cause problems that are hard to find and because Alice tends to perform poorly when methods contain infinite loops, but more importantly there is a better alternative! The better alternative is an event and it replaces an infinite loop as follows:[LIST=1][*] Write the method that you want to run continuously without an infinite loop.[*] At the top of the Events section click on “create new event” and choose the “When the world starts” event.[*] Right click on the left side of the new event where the dotted pattern is, highlight the “change to” option and click on “While the world is running.”[*] Finally, left click on the pattern on the left side of your method in the “methods” section and drag the method into the “During” [/LIST]Now when the movie starts so will your method and it will continue to be restarted whenever its end is reached. The running of other methods will not be affected and everyone is happy.
A few comments on events:
The event used above has three sections Begin, During and End (Programming Alice refers to it as a BDE event.) You can use one all or none of these “slots” in your event. Obviously using none of the options is useless but the others can come in handy if there are things you want to do before your During method starts or afterward in order to “clean up” when the during method ends. For the “When the world starts” event we are discussing here the “Begin” method would be called before the user interacts with the movie and the “End” event would execute as the movie was shutting down. As I stated above if you don't have any set up or clean up things that need to be done, just leave the slots blank.
There are other events that can be used to allow a method to be run only when certain conditions are met or when a variable changes. These events can be used to do things like keep an object from going too high or too low or almost any other condition you can think of and code.
[CODE]
While dragon is within(subject=dragon's height / 2) of ground is true
Begin: Nothing
During: dragon.moveUp
End: Nothing
[/CODE]
In these situations it is better to use a general condition rather than something specific. For instance, if instead of using “dragon is within(subject=dragon's height / 2) of ground” I were to use “dragon distance above ground more... == (subject = dragon's height / 2)” the event would only be called if, when the event next checked, the dragon was EXACTLY “dragon's height/2” above the ground. If, when the condition was evaluated, the dragon was higher or lower than ½ its height above the ground, no action would be taken.

Another way to use these events is for things like making the helicopter blades spin when you want them. Using a Boolean variable as a flag to determine when to let the blades turn.
[CODE]
While helicopter.spinBlades is true
Begin: helicopter.spinUp
During: helicopter.spinning
End: helicopter.spinDown
[/CODE]
In this example all three of the method “slots” are used. When helicopter.spinBlades ( which is a Boolean variable I created) becomes true, the Begin method (helicopter.spinUp) is called causing the blades to spin slowly at first and then faster and faster. When the helicopter.spinUp method completes, the event automagicly calls the “During” method (helicopter.spinning) and continues to call this method as long as the movie is running and helicopter.spinBlades is true. If at any time helicopter.spinBlades changes to false, maybe because the helicopter has landed or its engine has failed, then the “End” method is called, in this case causing the blades to slow down and stop.

One last point, if an infinite loop is used in a method the only way to call other methods is to contain them in a “Do together” block with the infinite loop. By default methods act sequentially, one after another, so Alice calls a method and then waits for it to end before going on to the next or instruction. In the case of a method with an infinite loop the method will never end because the loop will never end and so any methods or instructions following that method will not be executed. The only instructions that could be called are ones that are either in a “Do Together” block with our infinite instruction or ones that are in events become true or “fire.”
[CODE]
world.my first method
Do together
print This print will execute once.
While true
print While true is always true and will never stop.
Wait 5 seconds
// End of Do together
print This print will never execute.
[/CODE]

All times are GMT -5. The time now is 12:15 PM.

Copyright ©2022, Carnegie Mellon University
Alice 2.x © 1999-2012, Alice 3.x © 2008-2012, Carnegie Mellon University. All rights reserved.