Hello!
I found a script called " slice augmented menu" on the wiki that changes a slice depending on what menu item is selected.
I'm trying to alter it to use it without the editor's slice collections (they are confusing me).
It seems to work somewhat: But there is a slice to much.
I do not understand why there is the upper sprite shown. (It is marked with the question mark).
Where does it come from? And how to remove it?
plotscript, slice augmented menu, begin
open menu(1)
variable (menu, selection, img_1, img_2, img_3)
menu := top menu
while (menu is open(menu)) do (
selection := menu item true slot(selected menu item(menu))
switch (selection) do(
case (0) do (
free slice(img_2)
free slice(img_3)
img_1 := load hero sprite(0)
wait for slice(img_1)
put slice(img_1, 100, 150)
wait
)
case (1) do (
free slice(img_1)
free slice(img_3)
img_2 := load hero sprite(9)
wait for slice(img_2)
put slice(img_2, 100, 150)
wait
)
case (2) do (
free slice(img_1)
free slice(img_2)
img_3 := load hero sprite(11)
wait for slice(img_3)
put slice(img_3, 100, 150)
wait
)
)
)
free slice(selection)
end
Firstly, "wait for slice" does nothing here. It's for waiting for slice movements. Also, you wrote "free slice(selection)" but selection isn't a slice handle! And if you run using "Test Game" then you'll also see error messages about using "free slice" on a slice that doesn't exist.
The problem is that you load a sprite over and over and never free it. Each tick, you load another slice and it appears over top of the previous copy, so you don't notice. For example you free img_2 and img_3, but not img_1. Instead, it's easiest to free the previous slice every tick and then recreate it:
plotscript, slice augmented menu, begin
open menu(1)
variable (menu, selection, img)
menu := top menu
while (menu is open(menu)) do (
# Free the previous image
if (img) then (free slice(img))
selection := menu item true slot(selected menu item(menu))
switch (selection) do(
case (0) do (
img := load hero sprite(0)
put slice(img, 100, 150)
)
case (1) do (
img := load hero sprite(9)
put slice(img, 100, 150)
)
case (2) do (
img := load hero sprite(11)
put slice(img, 100, 150)
)
)
wait(1)
)
free slice(img)
end
Yes, that's what the while ... wait(1) does. You could instead wait for a keypress and only then free the old slice and create a new one, but there's no point because it's more work for no benefit.