This.hits bug

So, I was trying to make a game with the this.hits function and I got an error saying this:
Screenshot 2021-01-14 at 9.58.21 AM
what’s going on here?

Hey @tazer_900, what object is this on?

this.hits() only exists on clips and buttons. If this is being called inside a frame, then it will not work!

But I put the method in a clip

Would you mind sharing your project so I can take a look?

Here you go, it’s in the player clip:
My Project1-14-2021_9-40-24.wick (2.5 KB)

Alright, so this is a tricky one.

This bug happens because you are using the “function” syntax.

Because you are using this.hits inside a function, this gets kind of lost in JavaScript.

By pulling this code out of the function, you should have access to this.hits

Alright, thx!

You could also pass
this
As a funtion argument if needed. There are some workarounds.

this is how I do these kinds of things. I require passing in a clip. the first parameter is usually “this”. when necessary, i can always have more parameters.

In this example, project.myFunc does something if this touches otherItem.

project.myFunc = function(obj, target) {
    if(obj.hits(target)) {
        // stuff
    }
}

// if this touches otherItem, do something
// (but in a function instead of writing out
// the whole statement)
project.myFunc(this, otherItem);

you can also do this:

var clip = this;

function func() {
    if (clip.hits(target)) {
        //stuff
    }
}
2 Likes