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 Answer 1

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$ Commented Dec 8, 2019 at 9:01

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.