Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

button to move another object

A topic by Zillah Reynard created 5 days ago Views: 67 Replies: 4
Viewing posts 1 to 4
(+2)

Hello all! Is there a way to make it so that pressing a button will cause an image to move to a different position? I assume there is, but I'm having a hard time grasping movement in Decker.

Thank you!

(+2)

What kind of image do you want to move?

If your image is in a Canvas widget, you can move it around by setting the widget’s .pos attribute. Inside the button’s script:

on click do
 mycanvas.pos:12,34
end

…will move the top-left corner of the widget named mycanvas to 12 pixels from the left edge of the card, and 34 pixels from the top.

To figure out the coordinates you actually want, you can just tweak the numbers until it looks right. Alternatively, in any mode besides Interact, you can enable “Show Cursor Info” in the View menu, which will draw the mouse cursor’s coordinates next to the cursor. Point at where you want the image to go, copy the coordinates onto a scrap of paper or something, then edit the button script to match.

Note that if you move a widget like this, it stays moved if you save the deck. You may need another button to move it back to the original position if it’s part of a puzzle or a special effect or something.

Developer(+2)

As a little refinement, instead of hardcoding the coordinates, consider making a button widget named (say) "target1", setting it to "show none", and then referencing the position of that widget when repositioning the canvas:

on click do
 mycanvas.pos:target1.pos
end

Using a "tangible" object to represent positions and sizes on a card will often make it easier to make changes.

(+2)

You've got some good answers already but here's another style of moving canvases around.

If you'd like to have things visibly move from one location to another, like a smooth slide across the screen, I sometimes use the Ease module for that. You can find ease.deck in the examples folder that comes along with Decker. You'll need to transfer the ease module from that example deck into your project.

An example of creating this kind of animation with the Ease module might look like this:

on click do
 time:60
 each i in range time
  mycanvas.pos: ease.lerp[i/time-1  startref.pos  endref.pos]
  sleep[]
 end
end

To explain what's going on here:

The "time: 60" creates a temporary variable called time which is used to set how many frames the animation should take. Decker runs at 60 frames per second so this example goes pretty fast. Feel free to change the number to whatever you want.

The "each" loop will run repeatedly, for as many loops as the number in our "time" variable.

The mycanvas.pos: part starts just like the other examples in the thread, by setting the position of the image (canvas) you want to move... 

But in this example I'm calling on the ease module to help do the math for a specific style of movement. There's a bunch of different functions representing different kinds of movement in the module. Smooth, elastic, bouncy, etc. You can see all the included possibilities if you open up ease.deck. I chose lerp for this because it's smooth and simple but you can replace lerp with any of the other options.

The startref.pos and endref.pos inside of the brackets are our start and end positions. I wrote it as if they were referencing other widgets like Internet Janitor mentioned or they could be replaced with pairs of coordinates.

sleep[] pauses for a frame on each step of movement while the "each" loop is running. This is needed to make the animation take the amount of time we set earlier.

Then the two ends close off the "each" loop and the original "on click do" event handler.

I hope this makes some sense. It may not be what you're looking for right now, I just wanted to mention it.

(+3)

I'm going to try all of these and see which works best for me. Thank you all! This is really helpful.