0
$\begingroup$

So I have this code right here.

DynamicModule[{points = {}}, 
     EventHandler[Dynamic[points], {"LeftArrowKeyDown" :> (
         AppendTo[
          points, {RandomInteger[{-25, 25}], RandomInteger[{-25, 25}]}]
         ),
       "RightArrowKeyDown" :> (
         Drop[points]
         )}]]

The goal is to add an element to the "points" list whenever LeftArrow is pressed, and remove its last element whenever RightArrow is pressed. Everything is fine with adding elements, but nothing happens when I try to drop. Any ideas?

$\endgroup$
1
$\begingroup$

Two issues.

  1. Syntax for Drop is incorrect.
  2. You are not assigning the result of Drop to points.

Replace "RightArrowKeyDown" action with

points = Drop[points,-1]

AppendTo appends to the symbol passed. Drop merely returns the result of the drop; the symbol is unaffected. Therefore you must assign back to the symbol.

Hope this helps.

$\endgroup$
1
  • $\begingroup$ Yeah, I forgot to add -1 when I pasted the code here. Thanks a bunch! $\endgroup$ – 12345 Dec 8 '19 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.