[Make it easier] Switching events

For questions about using Classic.

Post » Mon Jan 04, 2010 6:13 pm

Hi.

:idea: 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:

:arrow: 1st method.



:arrow: 2nd method.



:!: 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?

~~~

:arrow: 3d method.



:!: This method works fine.

:?: Question: is there any way to accomplish the task easier ?
B
3
S
2
G
3
Posts: 146
Reputation: 1,567

Post » Mon Jan 04, 2010 6:51 pm

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.
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Mon Jan 04, 2010 7:32 pm

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.
B
3
S
2
G
3
Posts: 146
Reputation: 1,567

Post » Mon Jan 04, 2010 7:45 pm

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.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Mon Jan 04, 2010 7:54 pm

[quote="Arima":meobd8n6]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.[/quote:meobd8n6]

I understand, but how to force it works properly?
B
3
S
2
G
3
Posts: 146
Reputation: 1,567

Post » Mon Jan 04, 2010 8:07 pm

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.
Moderator
B
87
S
32
G
33
Posts: 3,005
Reputation: 27,397

Post » Mon Jan 04, 2010 8:15 pm

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
B
3
S
2
G
4
Posts: 1,445
Reputation: 4,665

Post » Mon Jan 04, 2010 8:17 pm

1st method

You first Disabled the "on" group yet it have to enable "off" group first.
[code:3a6x38vg]
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]
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Post » Mon Jan 04, 2010 8:48 pm

[quote="Arima":c16ns8cp]
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[/quote:c16ns8cp]

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



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

~~~

[quote="Madster":c16ns8cp]on layout start disable the off group[/quote:c16ns8cp]



I tried this but it doesn't work.

[quote="Doppel":c16ns8cp]1st method

You first Disabled the "on" group yet it have to enable "off" group first.
[code:c16ns8cp]
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][/quote:c16ns8cp]

I tried this but it doesn't work too.

This is a cap file: download.
Could you download it and make necessary changes?
B
3
S
2
G
3
Posts: 146
Reputation: 1,567

Post » Mon Jan 04, 2010 8:57 pm

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

[code:2wayqkbs]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.
B
2
S
1
G
4
Posts: 156
Reputation: 1,612

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests