How do I use JavaScript to determine if mouse is over an object and update an effect parameter?

0 favourites
  • 7 posts
From the Asset Store
Total customisation of the input! You can combine inputs from all peripherals. Make your game accessible for everyone!
  • Hello,

    I read through the scripting reference documentation but am not finding what I need.

    What I would like to do is this:

    1. Determine if the mouse is over object A
    2. If yes, enable effect "Brightness"
    3. Also set effect "Brightness" parameter 0 to 80
    4. Else, if the mouse is not over object A, disable effect "Brightness"

    So far, I am able to do this using the visual programming language of Construct3.

    I would like to be able to do this using JavaScript.

    I suppose I could use getMousePosition to get [x,y] and then check if the values are within the range of the object's coordinates?

    Then, how do I get the effect and set parameter 0 for "Brightness"? So far I have not found a way.

  • Anyone have any suggestions or can point me to some detailed guides please?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It looks like there is no easy way to do it with scripting. You can get mouse position, but you'll need to test all object instances if they are overlapping this position. And with different collision polygons in sprites, this may be a very difficult task. Edit: not that difficult, as Mikal pointed out, there is containsPoint(x, y) method.

  • You can try this, as dop2000 said, you can go through all the instances you want to check and use these methods to check if the mouse is over the collision poly of the instance and then use the effects api to control the instance's effects.

    construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/object-interfaces/iworldinstance

    - containsPoint(x, y) (check if the mouse x,y is within the collision poly of the instance)

    - effects (returns an object with the details on the effects on the instance)

    construct.net/en/make-games/manuals/construct-3/scripting/scripting-reference/object-interfaces/ieffectinstance

    - setParameter(index, value) (set effect parameter)

  • Personally, I found it the easiest to abstract the input events into an event sheet and then actually process them via the scripts (i.e. functions).

    Sadly, I did not find Mikal implementation suitable for my needs - the containsPoint does not take into account the user resizing their window.

  • Interesting, it seems different from my experience, but maybe you mean something different, I can resize the preview window and it still has the correct behavior. Is that what you mean by 'user resizing the windows'? I wonder what is different.

    Here's my example: gofile.io/d/qulRnW

    The code is simple:

    let spriteInst = null
    let spriteList= runtime.objects.Sprite.getAllInstances()
    let mouseInst = runtime.objects.Mouse
    for (let i=0;i<spriteList.length;i++)
    {
    	spriteInst = spriteList[i]
    	if (spriteInst.containsPoint(mouseInst.getMouseX(),mouseInst.getMouseY()))
    	{
    		spriteInst.opacity = 0.5
    	} else
    	{
    		spriteInst.opacity = 1.0
    	}
    }
  • Interesting, it seems different from my experience, but maybe you mean something different, I can resize the preview window and it still has the correct behavior. Is that what you mean by 'user resizing the windows'? I wonder what is different.

    Here's my example: gofile.io/d/qulRnW

    The code is simple:

    > let spriteInst = null
    let spriteList= runtime.objects.Sprite.getAllInstances()
    let mouseInst = runtime.objects.Mouse
    for (let i=0;i<spriteList.length;i++)
    {
    	spriteInst = spriteList[i]
    	if (spriteInst.containsPoint(mouseInst.getMouseX(),mouseInst.getMouseY()))
    	{
    		spriteInst.opacity = 0.5
    	} else
    	{
    		spriteInst.opacity = 1.0
    	}
    }

    That's definitely a working option, ain't thought of that. I was trying to determine the mouse position from the click event which seems to be your regular JS click.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)