Post new topic    
Liquid Metal Slime
Send private message
How could I create an 'UNDO' button? 
 PostThu Nov 07, 2013 6:46 am
Send private message Reply with quote
My current program allows me to place images on the screen every time I click the mouse. This placement uses the code:
Code:
sl := load large enemy sprite(a)


So if I were to place an image on the screen what would be the best way to then remove it (UNDO)?
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Liquid Metal King Slime
Send private message
 
 PostThu Nov 07, 2013 3:29 pm
Send private message Reply with quote
In order to remove a slice, you need to have a handle to it.

One possible method is to have a global variable that you use to store the last placed handle

Code:
global variable(73, last sl)


And then when you place the sprite slice, you store the handle

Code:
sl := load large enemy sprite(a)
last sl := sl


So later when you press the undo button, you can remove the slice using that saved handle

Code:
free slice(last sl)


The problem with this method is that it only works for the most recently placed slice, so you only get one level of undo. If you want to allow more levels of undoing, you can use a different approach.

Each time you add a new slice, it gets added as a child to the "sprite layer" (which is a container slice the same size as the screen)

You can get a handle to the most recently added slice like this:

Code:
variable(recent sl)
recent sl := last child(sprite layer)
free slice(recent sl)


This will work for all the slices parented to the sprite layer, so you will have unlimited levels of undo.
Liquid Metal Slime
Send private message
 
 PostThu Nov 07, 2013 3:47 pm
Send private message Reply with quote
Excellent. I think I'll attempt to implement the unlimited undo code this weekend. Thanks a lot!
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Display posts from previous: