1
$\begingroup$

I have a problem, that is I want to use loop to create multiple EventHandlers, but my code shows nothing. What can I do?

    DynamicModule[{i, pos10 = {{}, {}}, pos11 = {{0, 0, 0}, {0, 0, 0}}, 
                   pos12 = {{0, 0, 0}, {0, 0, 0}}, pos20, 
                  pos21 = {{0, 0, 0}, {0, 0, 0}}, pos22 = {{0, 0, 0}, {0, 0, 0}}}, 
 Graphics3D[{

   For[i = 1, i <= 2, i++,
    Print[i]; 
    EventHandler[
     Dynamic[{Translate[Cuboid[], pos11][[i]]},  ImageSize -> Tiny]
     ,
     {"MouseDown" :> {(pos10[[i]] = Mean@MousePosition["Graphics3DBoxIntercepts"])}, 
      "MouseDragged" :> (pos11[[i]] = pos12[[i]] + Mean@MousePosition["Graphics3DBoxIntercepts"] - 
          pos10[[i]]), 
      "MouseUp" :> (pos12 = pos11)}];
    ]},
  PlotRange -> {{-3, 3}, {-3, 3}, {-3, 3}}]]
$\endgroup$
0
$\begingroup$

I've fixed your code basing on the knowledge from the links from comments. With addition that: Translate[Cuboid[], {{1, 1, 1}, {2, 2, 2}}] is not really a list of Cuboids so you need to move [[i]] inside.

DynamicModule[{
  pos10 = {{}, {}},
  pos11 = {{0, 0, 0}, {0, 0, 0}},
  pos12 = {{0, 0, 0}, {0, 0, 0}},
  pos20,
  pos21 = {{0, 0, 0}, {0, 0, 0}},
  pos22 = {{0, 0, 0}, {0, 0, 0}},
  posInt
  },
 posInt[] := MousePosition["Graphics3DBoxIntercepts"];


 Graphics3D[{
   Table[With[{i = i},
     EventHandler[
      Dynamic[{Translate[Cuboid[], pos11[[i]]]}],
      {"MouseDown" :> {pos10[[i]] = Mean@posInt[]},
       "MouseDragged" :> (pos11[[i]] = pos12[[i]] + Mean@posInt[] - pos10[[i]]),
       "MouseUp" :> (pos12 = pos11)}]],
    {i, 2}]},
  PlotRange -> 3]

 ]

Good luck.

$\endgroup$

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.