Skip to main content

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

Is it possible to have the drag event on a canvas affect a certain area of that canvas? Say:

on drag pos rect[(1,1) (10,10)] do
 [script here]
end

or test if the pattern of a pixel in a canvas is a certain one to execute a script?

on drag pos do
 if pattern = 1
  [script here]
 end
end
(1 edit) (+2)

You can test whether the drag position is within a given rectangle like this:

if min (pos > 1,2),(pos < 10,20)
 alert["You clicked in the rectangle!"]
end

pos is an two-item list representing the X and Y coordinates of the pointer when the event happened. If we pretend there are separate x and y variables representing the individual coordinates, then pos > 1,2 is a shorter way to say (x > 1),(y > 2) - that is, you get a new two-element list where each item is 1 if the corresponding coordinate matches. In the same way, pos < 10,20 is like (x < 10),(y < 20). Put them together with “,” and you get a four element list where each item is 1 if the coordinate is inside that edge of the rectangle. min takes that four-element list and returns the minimum value: if any element is 0, it returns zero; it only returns 1 if every element is 1. That is, it only returns 1 if the point is completely within the rectangle.

It’s also possible to check the pattern in the canvas at a point, like so:

if me[pos] = colors.red
 alert["You clicked a red pixel"]
end

(where me inside a widget’s script refers to that widget, whatever its actual name is)

Does that help?

(4 edits) (+1)

Very much, thank you!

Though, now I have a new problem.

I got a canvas, a slider (Switch), and a field (Circle) with two images.

The Code for the canvas

on click pos do
 if me[pos] = colors.black
  Switch.value:Switch.value + 1
 end
end

And the Code for the slider

on change val do
 if Switch.value = 1
  canvas1.paste[Circle.images[1]]
 else
  canvas1.paste[Circle.images[0]]
 end
end

When a black pixel is clicked, it's supposed to change the value of Switch from 0 to 1 then paste the image of that value from Circle into canvas1. It does the second step, but not the third. And the slider works when clicking on it but not when changed by another widget.

Figured it out! Here's the code:

on drag pos do
val:Switch.value
 if (me[pos] = colors.black) & (Switch.value = 0)
  Switch.value:Switch.value + 1
 elseif (me[pos] = colors.black)
  Switch.value:Switch.value - 1
 end
 Switch.event["change" val] 
end
(+1)

Yeah, assigning to a widget’s .value does not actually trigger the widget’s on change handler. That is a good thing - for example, you might have a slider and a field, so the player can enter a number by quickly scrubbing along the slider, or typing a precise number in the field. Changes to the slider need to set the field text, changes to the field text need to set the slider value, so if setting .value automatically triggered on change, it would get caught in a loop: changing the text would set the slider, which would change the text, which would set the slider… The way it works now, each field can just set the other’s value to match its own without any mishaps.

For what it’s worth, your code could be simplified a little. Since both arms of the if test me[pos] = colors.black, we can lift that condition out:

val:Switch.value
if me[pos] = colors.black
 if (Switch.value = 0)
  Switch.value:Switch.value + 1
 else
  Switch.value:Switch.value - 1
 end
end
Switch.event["change" val] 

That inner if statement now just makes Switch.value into 1 if it was 0, and 0 if it was 1. It turns out Lil already has an operator that does exactly that: the ! operator.

val:Switch.value
if me[pos] = colors.black
 Switch.value:!Switch.value
end
Switch.event["change" val] 

Lastly, I see you’re sending the “change” event with the original value, even if the new value is changed. Normally the “change” event gets called with the new value, so that can get confusing if sometimes it gets the new value (from the user) and sometimes the old (from code like this). Unless you specifically need it to be the original value for some reason, it’s probably better to send the new value - then you don’t need to save the original value at all. Also, the “change” event is typically only sent when something actually changes:

if me[pos] = colors.black
 Switch.value:!Switch.value
 Switch.event["change" Switch.value] 
end
(+2)

Thank you for the optimization!

Unironically, I love it when, indeed, a program could be simplified further.