is there any way of ensuring that a slice can be moved, but only within the bounds of its parent?
Is there any way of allowing slices to move as long as they are touching or within another slice?
making a slice bounded by/in another slice
Moderators: marionline, SDHawk
- BMR
- Metal King Slime
- Posts: 3310
- Joined: Mon Feb 27, 2012 2:46 pm
- Location: The Philippines
- Contact:
I'm guessing you'd check collisions to see if the slice is "within" the other one. There might be a better method, but that's the one that springs to mind. You'd check if they're colliding, and if they are, you allow movement. If they're not colliding, you prevent the movement. Something like.
GET INPUT
MOVE DIRECTION := INPUT DIRECTION
IF COLLIDING THEN
NEW LOCATION := OLD LOCATION + MOVE DIRECTION
ELSE
DO NOTHING
EN IF
Or something like that.
GET INPUT
MOVE DIRECTION := INPUT DIRECTION
IF COLLIDING THEN
NEW LOCATION := OLD LOCATION + MOVE DIRECTION
ELSE
DO NOTHING
EN IF
Or something like that.
Last edited by BMR on Tue May 02, 2017 2:39 am, edited 2 times in total.
Being from the third world, I reserve the right to speak in the third person.
Using Editor version wip 20170527 gfx_sdl+fb music_sdl
Using Editor version wip 20170527 gfx_sdl+fb music_sdl
Use 'slice collide' to check if two slices intersect. Use 'slice contains' to check if one slice is entirely within another. You can also use 'clamp slice' to move a slice so that it's contained within another.
So, if you want to use normal slice movement commands, you could follow them with a while loop that checks whether the slice is still within the desired bounds, using slice contains/slice collide, and then call 'stop slice' when it leaves those bounds. Unfortunately that will leave it one step outside the bounds you want. You would have to manually move the slices, like BMR wrote, to avoid that.
So, if you want to use normal slice movement commands, you could follow them with a while loop that checks whether the slice is still within the desired bounds, using slice contains/slice collide, and then call 'stop slice' when it leaves those bounds. Unfortunately that will leave it one step outside the bounds you want. You would have to manually move the slices, like BMR wrote, to avoid that.
- Bob the Hamster
- Liquid Metal King Slime
- Posts: 7460
- Joined: Tue Oct 16, 2007 2:34 pm
- Location: Hamster Republic (Ontario Enclave)
- Contact:
I made a little demo of the "clamp slice" command.
You can download the example rpg file here: https://www.slimesalad.com/forum/viewgame.php?p=128478

Code: Select all
global variable(1, sprite)
global variable(2, box)
global variable(3, button)
plotscript, clampslice example, begin
suspend player
init mouse
unhide mouse cursor
load slice collection(0)
sprite := lookup slice(sli:dinosaur)
box := lookup slice(sli:box)
button := lookup slice(sli:button)
while(true) do(
if(keyval(key:esc) > 1) then(game over)
update button()
set slice edge x(sprite, edge:center, mouse pixel x -- slice x(box))
set slice edge y(sprite, edge:center, mouse pixel y -- slice y(box))
clamp slice(sprite, box)
wait(1)
)
end
script, update button, begin
if(slice collide point(button, mouse pixel x, mouse pixel y)) then(
set rect bg col(button, random(0, 255))
if(mouse click(left button)) then(
randomize the box()
)
)else(
set rect bg col(button, 96)
)
end
script, randomize the box, begin
variable(width)
width := slice width(sprite layer)
variable(height)
height := slice height(sprite layer)
set slice x(box, random(0, width / 4))
set slice y(box, random(0, height / 4))
set slice width(box, random(width / 2, width * 3/4))
set slice height(box, random(height / 2, height * 3/4))
end
