How do I realize mouseenter and mouseleave events?

0 favourites
  • 13 posts
From the Asset Store
14 amazing sound files of game events like Level Ups, Level Completes, object spawn, object taking etc.
  • I have many instances of object with states of "default" and "highlight" and I need to change states of exact instance when mouse cursor enters its area and leave it.

    I do not want to check it every tick, but when event triggers.

    I tried multiple approaches and cant do it "right" and without JS-code. Im a poor programmer and cannot understand how JS-code works in Construct3 =\</p>

  • It's easy to do with events running on every tick. If you worry about the performance, test it, I'm sure it will be fine.

  • You can use trigger once to stop running the event after activating, effectively making it a triggered condition/event.

  • If there are multiple object instances, I don't recommend using Trigger Once, it may work incorrectly. Besides, the condition will still be evaluated on every tick, so it won't give any performance increase.

  • All trigger conditions are evaluated constantly aren't they?

  • I CAN use use events every tick, but it does not cover my needs.

    For example I have 4 custom "buttons" with sprite animations 0 as default and 1 as highlight very close to each other.

    I have events "Mouse Cursor is over Button -> Set animation to 1" and "Mouse [NOT] Cursor is over Button -> Set animation to 0".

    If I VERY FAST move cursor from fist to fourth "button" all objects will be highlighted.

    So I need to reset animation to 0 when cursor left object to prevent that behavior.

    It easily done with CSS/JS, but I dont understand how to write custom JS function in Construct3, or cant find built-it visual-code element for mouseenter and mouseleave events.

    P.S. Yes, in theory I can implement some variables to check active element and reset all non-active, but it seems to be invalid approach.

  • This shouldn't happen if you organize the events correctly. Try this:

    On Every tick: Button Set frame to 0
    
    (sub-event)
    .... Mouse over Button : Button Set frame to 1
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Nice. But what if my button is a container with a text attached to it, which get offset +1 +1 when cursor is hovered? If i set on every tick return offset by -1 -1 my text will fly away.

    I need to register mouseleave event =((

  • Same way - on every tick set text to its default position for all buttons.

    If mouse is over the button - offset text position.

  • Utilize a very simple statemachine.

    wackytoaster.at/parachute/buttonstatemachine.c3p

  • State machine is nice... but it check ALL objects in family EVERY frame!

    Thats doesnt feel right.

  • Don't worry about it, it is right.

    You see, mouseenter is an event that is triggered when the mouse first enters an object. How do you know if the mouse just entered an object? It has to "not" be on the object before, and then once it is over the object, the trigger is fired. How do you check if the mouse is (not) over the object? You ask, every tick, "Is mouse over object". So while yes, the mouseenter event is only fired once, it still has to evaluate the mouse position every tick to check if it is or isn't over the object.

    It's similar to constructs "On collision" which is a trigger condition, but you only can know that two objects collide if you check for it every tick. So "On collision" will actually still require collision checks every tick, but the trigger is only called once.

    Unless you are talking thousands of buttons you need to check every tick, this will not be an issue even on mobiles.

  • Sprites are not dom elements so they can't have js events such as mouseenter added to them. But either way to logic is the same: all the sprites would need to be checked to see if the mouse is overlapping them every frame, unless there is some spatial partitioning done behind the scenes.

    Anyways, simpler is often better (and faster) with events. Like others have stated you can do this which will will access all the sprites twice per frame. You'll run into other performance issues with a high number of instances in other ways before this is an issue.

    every tick -> sprite: set highlight to false
    mouse over sprite -> sprite: set highlight to true

    You can reduce it to accessing all the instances only once per frame with logic like this:

    var prevUid=-1
    
    sprite: pick by uid prevUID
    -- set highlight to false
    -- set prevUid to -1
    
    mouse over sprite
    -- sprite: set highlight to true
    -- set prevUid to sprite.uid

    The final idea is to use some kind of spatial partitioning which could in theory reduce the number of instances to check to one per frame. For example here is a grid hash idea.

    dropbox.com/s/n21w4qwgp1k9tkt/spacial_hash2.capx

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