[Make it easier] Switching events

This forum is currently in read-only mode.
0 favourites
From the Asset Store
Toggles Dials Switches and Alerts ! A foundational sound grouping of Analog type switches and Steampunk effects
  • Hi.

    In this topic I'd like to write my methods of solving a particular problem and discuss with you how it could be made more easily and properly.

    The first task:

    We need to make that if we press ''Space'' sprite moves up to 64 pixels, and then if we press ''Space'' again it returns to the original position (moves down to 64 pixels) and so all the time.

    I made it by 3 methods:

    1st method.

    <img src="http://i45.tinypic.com/10hk4rq.jpg">

    2nd method.

    <img src="http://i48.tinypic.com/11vp6s3.jpg">

    These two methods work but not properly � when I run the application and press ''Space'' the first time nothing happens. But if I press it again � sprite moves and events begin to work .

    Question: why it doesn't work the first time?

    ~~~

    3d method.

    <img src="http://i45.tinypic.com/c7pk4.jpg">

    This method works fine.

    Question: is there any way to accomplish the task easier ?

  • the second method doesn't work the first time because both event groups are enabled.

    At layout start, instead of enabling "on" you should be disabling "off".

    I use the method with variables, I hadn't considered using groups, it looks more readable but it could get messy if states are more than 2.

  • Madster

    Do you know why the first method doesn't work properly? May be it is a bug?

    I use the method with variables too, but I'd like to find the method not using any variables and easier than 2nd method.

  • The first method doesn't work because you enable the group "off" after running the first event, which means that the second event is run in the same frame as the first, moving it back before the screen is drawn.

  • The first method doesn't work because you enable the group "off" after running the first event, which means that the second event is run in the same frame as the first, moving it back before the screen is drawn.

    I understand, but how to force it works properly?

  • I use a variable. For example:

    if space is down

    • Set variable 'space' to 1

    if space is not down

    • Set variable 'space' to 0

    If space is pressed

    If variable 'space' is 1

    • actions - set space to 0

    The next event that checks if space is pressed, if it's 0, it won't run. There's probably a better way but that's how I do it.

  • first method is lacking start conditions.

    on layout start disable the off group.

    2nd method is overkill, I meant the first one XD I'm sick and kinda feverish

  • 1st method

    You first Disabled the "on" group yet it have to enable "off" group first.

    Sprite: Set Y to Sprite.Y-64
    System :Disable group "on"
    System :Enable group "off"[/code:3a6x38vg]
    
    Should be [code:3a6x38vg]
    Sprite: Set Y to Sprite.Y-64
    System :Enable group "off"
    System :Disable group "on"
    [/code:3a6x38vg]
  • if space is down

    - Set variable 'space' to 1

    if space is not down

    - Set variable 'space' to 0

    If space is pressed

    If variable 'space' is 1

    - actions - set space to 0

    I've tried your method, here is a screenshot:

    <img src="http://i47.tinypic.com/x3cqol.jpg">

    This method doesn't work or I didn't understand you... Could you make an example?

    ~~~

    on layout start disable the off group

    <img src="http://i46.tinypic.com/10ni5fk.jpg">

    I tried this but it doesn't work.

    1st method

    You first Disabled the "on" group yet it have to enable "off" group first.

    > Sprite: Set Y to Sprite.Y-64
    System :Disable group "on"
    System :Enable group "off"[/code:c16ns8cp]
    
    Should be [code:c16ns8cp]
    Sprite: Set Y to Sprite.Y-64
    System :Enable group "off"
    System :Disable group "on"
    [/code:c16ns8cp]
    

    I tried this but it doesn't work too.

    This is a cap file: download.

    Could you download it and make necessary changes?

  • If you want to make a toggle switch just set the initial variable to 1.

    than :

    On space pressed >
        VariableSwitch = VariableSwitch * -1
    
    //so you get 1 or -1 every space press.
    
    //than compare:
    
    if  VariableSwitch = -1
        Do Action
    
    if  VariableSwitch = 1
       Do Action[/code:2wayqkbs]
    
    that's what I always do.
  • <img src="http://i45.tinypic.com/10hk4rq.jpg">

    The reason this one doesn't work is because events are executed top to bottom. The logic is more like this:

    If group on is activated
             If space was pressed
                Disable group on and enable group off
    If group off is activated  -  (which the previous line just activated)
            If space was pressed
               Disable group off and enable group on[/code:2ljvzfef]
    
    Because group 'off' is enabled in event 1, in the same cycle the second event runs.
    
    Another solution might be to put the 'space is pressed' code outside the groups and use 'toggle group'
    
    eg:
    
    [code:2ljvzfef]
    If group on is activated
             If space was pressed
                do stuff
    If group off is activated
            If space was pressed
               do stuff
    If space was pressed
          toggle group "on"
          toggle group "off"
    [/code:2ljvzfef]
  • funny though, it works after the first round.

    Perhaps disabling/enabling groups only goes effective on the next frame and this is a bug? (on default-disabled groups not really being disabled?)

  • <img src="http://i47.tinypic.com/x3cqol.jpg">

    Agh, sorry, I misinterpreted what you were trying to do, and messed up the example regardless. Here it is fixed:

    event 1 conditions:

    • On space pressed

    event 1 actions:

    • set 'spacepressed' to 1

    event 2 conditions:

    • If variable 'spacepressed' is 1
    • If variable 'on' is 1

    event 2 actions:

    • Sprite: set x position to sprite.x+64
    • set 'spacepressed' to 0
    • set 'on' to 0

    event 3 conditions:

    • If variable 'spacepressed' is 1
    • If variable 'on' is 0

    event 3 actions:

    • Sprite: set x position to sprite.x-64
    • set 'spacepressed' to 0
    • set 'on' to 1

    event 4 conditions:

    • Always

    event 4 actions:

    • set 'spacepressed' to 0

    If that doesn't work I'll make an example.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • wow these are all really really complicated for something really simple....

    -> on space pressed: switchVar = switchVar * -1; //Starting Value of switch var = -1
    -->If SwitchVar == 1: sprite.x += 64 //adds 64 to the current X value
    -->if Switchvar == -1 : sprite.x -= 64 // subtracts 64 from the current X value[/code:1s1c60b8]
  • yeah its a flow control issue. this worked too. If it was more than one state, It would be difficult.

    + MouseKeyboard: On any key pressed

    + System: Is global variable 'lulz' Equal to 0

    • > System: Set global variable 'lulz' to 1
    • > Sprite: Set X to Sprite.X + 64

    + System: Else

    • > System: Set global variable 'lulz' to 0
    • > Sprite: Set X to Sprite.X - 64
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)