If buttons overlap, goto frame

I want to know how to efficiently code a clip so that it triggers a timeline move on overlap with another object.

The scenario is a game wherein an animated character is moving on the canvas and you bring it to a goal state, via keyboard input. Something like a “mouse” being meant to find a “cheese” and then it says “you win” or goes to another board.

I’ve been able to execute this idea in a sub-optimal way with a onmouse enter, but wherein, the animated clip had been blocking the mouse, so by either having the animation dodge the mouse center or skip frames I can get it to “kind of work”. Still, li’d like to use keyboard controls,too.

I’ve looked up some javascript suggestions that involve the math of computing the distance between the center of the objects, but that seems like overkill.

I’m wondering if there is a simple command like, “if objectA intersects objectB then, goto…”

1 Like

It sounds like you could accomplish this with the hitTest command. It’s meant to test collision between two objects, like this:

if (mouse.hitTest(cheese)) {
project.gotoNextFrame();
}

Here’s an example project with this code:
mouse-cheese-hitTest.wick (3.1 KB)
(Move the mouse with the arrow keys, when mouse touches the cheese the hitTest code executes)
That would most likely be the quickest and easiest way of doing it in Wick Editor.

Thank you, thank you.

1 Like

I forgot to mention this in the previous comment - the shortcut for adding the hitTest code can be found in the code panel References, under Object, all the way at the bottom.
Image53

Image54

1 Like

Welcome @Sean_Mills to the forums. : )

1 Like

probably a more efficient way is to simply check your clip x and y positions in relation to the target object
that’s probably the math you are trying to avoid but it’s easy

tolerance= 10; //set your tolerance
if (Math.abs(playerclip.x-target.x)<tolerance && Math.abs(playerclip.y-target.y)<tolerance){
// playerclip is close enough to target. do something
}

you can check distance easily against a list of objects in an array

remember that hittest usually do alot of calculations and tend to slow down games

1 Like

I agree that using comparison code instead of a hitTest would be more efficient, at least in terms of performance. However, this method of comparing the x and y values with Math.abs has drawbacks as well. This compares the center points of the two objects. I think this would work very well for squares and similar shapes, but I found difficulty in trying to get it to correctly gauge the distance of something less simple.
Say I have these two objects, and I want to test if they overlap. The black circle should turn yellow when the cloud touches it. With the tolerance set to 10, it doesn’t work at this distance:
Image61
Because even if the objects overlap, the center points don’t. So, let’s set the tolerance higher, to 70. Now it works, but at a distance where it doesn’t look like the objects overlap yet:
Image63
It seemed to work best when the tolerance set to a value between 60-65, but even then I could still run into these kinds of issues. It’s simply not taking into account the boundaries of the shape itself, just the distance between the center points. I think this could certainly be effective in some apps, sometimes all you really want is an approximation of distance. But the hitTest code isn’t just simpler to write, it’s more accurate for unusual shapes. Just my thoughts on the issue.

1 Like

In my particular case, I’d like to focus on the visual elements and concepts and get my collaborators to improve (where necessary or useful) the coding.

I’m curious about the practical benefits so I can at least explain them :slight_smile:

So, for my immediate purpose I think “hitTest” is just what the Doctor ordered, but if I find out that calculating relative target locations make things 100x faster or less likely to crash or something, that’s always good knowledge for the back pocket.

Thanks again, to all. This is not the place, but WICK.EDITOR (and this forum) is letting me pick up where I left off with Actionscript 2.0 like 15 years ago.

1 Like

Both are valid ways of going about it, ultimately it comes down to what kind of project you’re working on and what your priorities are. :slightly_smiling_face:
On a side note, I used to work with ActionScript 3 in Adobe Animate before finding Wick Editor. I never got very far working in Animate, but I’m glad I discovered Wick Editor and have resumed exploring coding at my own pace. (I always felt like I wasn’t improving quickly enough while using Animate to justify it’s roughly $30 per month cost)

We are in the same boat. I’m pretty sure that you will enjoy this editor.

1 Like