Alice Community  

Go Back   Alice Community > Alice 2 > Bugs and Troubleshooting

Reply
 
Thread Tools Display Modes
While Loop and Keypress Listener
Old
wbarnum
Member
 
Status: Offline
Posts: 20
Join Date: Jun 2015
Default While Loop and Keypress Listener - 08-21-2015, 12:33 PM

I've written a program that is supposed to move a character forward as long as the "up" arrow is pressed.

I'm using a while loop with the condition "event isKey UP is true".

The character starts moving forward when I press the "up" arrow, but when I release it, the character continues moving forward indefinitely.

I don't know if this is a bug or a case of user error.
Attached Images
File Type: jpg While_Keypress.JPG (81.3 KB, 8 views)
Attached Files
File Type: a3p SealProgram.a3p (40.8 KB, 0 views)

Last edited by wbarnum; 08-21-2015 at 01:40 PM.
   
Reply With Quote
Old
MrMoke
Senior Member
 
MrMoke's Avatar
 
Status: Offline
Posts: 501
Join Date: Sep 2007
Location: Austin, Texas
Default 08-21-2015, 01:54 PM

User Error.
You don't need the while statement, just an if.
Examine the "add details" options in the header of the key press event, and experiment with those to see what happens.

http://www.alice.org/3.1/materials_videos.php

Last edited by MrMoke; 08-21-2015 at 01:59 PM.
   
Reply With Quote
Old
wbarnum
Member
 
Status: Offline
Posts: 20
Join Date: Jun 2015
Default 08-21-2015, 02:41 PM

Quote:
Originally Posted by MrMoke View Post
User Error.
You don't need the while statement, just an if.
Examine the "add details" options in the header of the key press event, and experiment with those to see what happens.

http://www.alice.org/3.1/materials_videos.php
Using an if has other undesirable consequences.

Is there any reason from a computer science perspective that a while loop shouldn't work here?

Last edited by wbarnum; 08-21-2015 at 02:59 PM.
   
Reply With Quote
Old
MrMoke
Senior Member
 
MrMoke's Avatar
 
Status: Offline
Posts: 501
Join Date: Sep 2007
Location: Austin, Texas
Default 08-21-2015, 06:31 PM

From a Computer Science Perspective, Yes Absolutely!
Your while loop is doing exactly what it is supposed to do. (ie: Execute repeatedly as long as the test is true)

Don't forget to review the link I added to my last post, and checkout the How-to guide

Last edited by MrMoke; 08-21-2015 at 06:41 PM.
   
Reply With Quote
Old
wbarnum
Member
 
Status: Offline
Posts: 20
Join Date: Jun 2015
Default 08-24-2015, 01:15 PM

Quote:
Originally Posted by MrMoke View Post
Your while loop is doing exactly what it is supposed to do. (ie: Execute repeatedly as long as the test is true)
When I let up on the key, wouldn't that make the condition false and it would stop executing the inner loop and therefore stop moving the character forward?
   
Reply With Quote
Old
MrMoke
Senior Member
 
MrMoke's Avatar
 
Status: Offline
Posts: 501
Join Date: Sep 2007
Location: Austin, Texas
Default 08-24-2015, 05:54 PM

Nope-
A key press triggers a call to the event, and the key value is passed in. If it is the key that you are looking for, the "while" will never stop because you are already in the event. Instead, focus on the "add detail" options in the "arrowKeyPressed" header.

Time to start Experimenting

Quote:
Originally Posted by wbarnum View Post
When I let up on the key, wouldn't that make the condition false and it would stop executing the inner loop and therefore stop moving the character forward?
   
Reply With Quote
Reply

Tags
event listener, loop

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



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