Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   How do I...? (http://www.alice.org/community/forumdisplay.php?f=16)
-   -   Need help with college work please! (http://www.alice.org/community/showthread.php?t=9774)

ekat90 12-11-2012 09:49 PM

Need help with college work please!
 
I'm trying to make a penguin game, where when I've controlled the penguin so that when the penguin is close enough to a fish, its commanded to say yum and for the fish dissapear, thats all fine but my only dillemma is even though I make the fish dissapear when I run the penguin over the invisible fish the penguin still says yum!. This is really really frustrating me and no matter what i read or do I cannot make the penguin only say it once, after its eaten it! please I'd appreciate any help.

ekat90 12-11-2012 11:20 PM

[URL="http://www.2shared.com/file/yDbI2spT/A1-Startup.html"]http://www.2shared.com/file/yDbI2spT/A1-Startup.html[/URL]

link too the work please have a look. I'm restricted I can't use variations, do together or do in order.

HaloMike117 12-12-2012 12:02 PM

[QUOTE=ekat90;51917]I'm trying to make a penguin game, where when I've controlled the penguin so that when the penguin is close enough to a fish, its commanded to say yum and for the fish dissapear, thats all fine but my only dillemma is even though I make the fish dissapear when I run the penguin over the invisible fish the penguin still says yum!. This is really really frustrating me and no matter what i read or do I cannot make the penguin only say it once, after its eaten it! please I'd appreciate any help.[/QUOTE]

Resolving this problem is quite simple by using an if statement to check if the fish has been eaten or not.

To do this, you'll want to create a boolean that checks if the fish is showing or not.
[CODE]
boolean fishDead()
{
if (fish.isShowing == true)
{
return true;
}
return false;
}
[/CODE]

Next, all you'll need to do is check if the fish is dead or not before executing the code where the fish says "yum!".

To do this, simply add the following if statement:
[CODE]
if (fishDead() == true)
{
//say("Yum");
}
[/CODE]

Simple as that!

Good luck (:

mcelroy32400 12-12-2012 05:28 PM

I like HaloMike's answer. Another cheap/jimmy-rigged way of doing it is to make the fish move up (or down) like 20 meters (or however far away you want) when the fish is eaten the first time. This way the penguin will never get close to it again. You could do it as a class method, something like fishEaten. Make it move up (or down) in 0 seconds and that will launch the fish away immediately, removing the possibility of the penguin getting too close ever again. (Another variation is to create a dummy object where all the fish could immediately get moved to once they've been eaten, again using 0 seconds to make it happen as though it is a do together without actually using the Do together.)

Like I said, HaloMike has the better programming solution.

Beans 12-12-2012 06:51 PM

REALLY easy thing to do:
Variables!
Make a variable: Fish_Eaten
or something, make it turn on if the
fish is eaten, and then add a infinite loop
of if Fish_Eaten is on do nothing, else do
(insert your code here) when you reach within a certain
spot of the fish.

ekat90 12-12-2012 09:18 PM

[QUOTE=HaloMike117;51924]Resolving this problem is quite simple by using an if statement to check if the fish has been eaten or not.

To do this, you'll want to create a boolean that checks if the fish is showing or not.
[CODE]
boolean fishDead()
{
if (fish.isShowing == true)
{
return true;
}
return false;
}
[/CODE]

Next, all you'll need to do is check if the fish is dead or not before executing the code where the fish says "yum!".

To do this, simply add the following if statement:
[CODE]
if (fishDead() == true)
{
//say("Yum");
}
[/CODE]

Simple as that!

Good luck (:[/QUOTE]

I'm obviously using alice I don't know how too add boolean (fishdead) I just got not a both a and b and either a or b, or both, i dont know how too add =isshowing. we've only been taught to click and drag methods and functions thus far.

The only way i thought you could tell alice its dead is by changing the opacity 0%. Sorry guys I'm a complete newbie I just hope i don't fail this assignment :confused:

[QUOTE=Beans;51934]REALLY easy thing to do:
Variables!
Make a variable: Fish_Eaten
or something, make it turn on if the
fish is eaten, and then add a infinite loop
of if Fish_Eaten is on do nothing, else do
(insert your code here) when you reach within a certain
spot of the fish.[/QUOTE]

the assignment restricts us from doing variables :(

HaloMike117 12-13-2012 11:39 AM

[QUOTE=ekat90;51940]I'm obviously using alice I don't know how too add boolean (fishdead) I just got not a both a and b and either a or b, or both, i dont know how too add =isshowing. we've only been taught to click and drag methods and functions thus far.

The only way i thought you could tell alice its dead is by changing the opacity 0%. Sorry guys I'm a complete newbie I just hope i don't fail this assignment :confused:



the assignment restricts us from doing variables :([/QUOTE]

My apologies, I didn't have alice open at the time and was unable to give a proper response.

A boolean is a type of function. You'll want to click the button that says create new function (I would recommend adding this function in your fish's functions rather than the world - as it is a common practice).

Next, type a name for the function. In your scenario, "fishDead" is a good name. Click the Boolean button and then select "OK".

Now that you have the boolean function created, you'll want to add code to it so when called, it performs a task. Drag the "If/Else" into the fishDead class. You'll see the following code:

[CODE]
if (true)
{
//Do Nothing
} else {
//Do Nothing
}
Return true;
[/CODE]

As you have said you used opacity (rather than the property isShowing) you will need to check the opacity of the fish. To do this, go to the fish's property tab and locate the property "opacity". Drag and drop this property on the "true" in the fishDead class. Alice will ask you to select from several options. Select the option that says "opacity ==" and then select "other...". A custom number box will come up. Enter 0 (zero) into this box and select "Okay".

Your code for the fishDead class should now look something like this:
[CODE]
if (fish.opacity == 0)
{
//Do Nothing
} else {
//Do Nothing
}
Return true;
[/CODE]

You will now want the code to return true or false. Since the if statement is asking if the fish's opacity is 0 (dead), you will return true signifying that the fish is dead. To do this, drag and drop the Return statement below the if statement and set it to "true".

Your code should now look something like this:
[CODE]
if (fish.opacity == 0)
{
Return true;
} else {
//Do Nothing
}
Return true;
[/CODE]

Finally, you'll want to return false in the event that the fish's opacity is not 0 (this returns that the fish is still alive). To do this, change the return statement at the bottom of the class to "false". It would also be a good idea to add in some comments so others know what the function does.

Your finished code should look something like this:
[CODE]
//Using the fish's opacity as a guideline, checks to see if the fish is dead or not.
//If the fish's opacity is 0, the fish is dead and the function is returned true.
//If the fish's opacity is not 0, the fish is still alive and the function is returned false.
if (fish.opacity == 0)
{
//The fish is dead, so we return true.
Return true;
} else {
//Do Nothing
}
//The fish is still alive, so we return false.
Return false;
[/CODE]

Now that you have a function created that checks if the fish is dead or not, you can use it in your code. Locate where the fish says "Yum!". You'll want to drag and drop an If/Else statement in that area. Next, you'll want to drag and drop the fishDead function onto the if statement so it looks like so:

[CODE]
if (fish.fishDead)
{
//Do Nothing
} else {
//Do Nothing
}
[/CODE]

Since your goal is to only say "Yum!" when the fish is dead, you'll need to check if the fishDead boolean is false, rather than true. To do this, click the arrow next to fish.fishDead, hover over "logic", then hover over "fish.fishDead<none> ==", and finally select "false".

The code should now look like this:
[CODE]
if (fish.fishDead == false)
{
//Do Nothing
} else {
//Do Nothing
}
[/CODE]

Great! Now that we have this, you'll want to perform an action only if the fish is alive. Drag and drop the "say" method into the if statement and set the text to "Yum!". It will look something like this:

[CODE]
if (fish.fishDead == false)
{
penguin.say("Yum!"); duration = 0 seconds
} else {
//Do Nothing
}
[/CODE]

There you go! Problem solved! The penguin will now only say "Yum!" when the fish is alive.

Don't forget to add in comments to your code as this is important.
I hope you understood everything we did here, so next time you'll be able to do it on your own. If for any reason you failed to understand something provided here, PLEASE let me know. I'll be more than happy to help out. :)


All times are GMT -5. The time now is 07:38 PM.

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