How do I clean up this code?

Get help using Construct 2

Post » Sun Feb 22, 2015 5:26 am

:idea: I came up with a way that lets me have fading layers and sprites of the same type setup across the different layers and have my mouse properly tell me which one is active, limited by which one is currently visible.

CAPX @ : http://d.pr/f/uTA7/2HMUdXCi

My question is... am I doing this in a bad way? Is there a better way I can do it? Is there a cleaner way I can do it? This is just a test version to show you how im doing it, but if I am going to go ahead with this in my actual code, it's going to be a LOT of garbagey stuff I feel like there must be a way more efficient way to get the same results?

Would madly appreciate any help, if you have any time to check.

Thank you!
Nate
Made Cosmochoria - www.cosmochoria.com
Currently working on Slayaway Camp - www.slayawaycamp.com
B
25
S
7
G
3
Posts: 375
Reputation: 4,852

Post » Sun Feb 22, 2015 5:36 am

Use functions. With return value. That way you only code once and can call/use that function(code) all over the place.
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,698

Post » Sun Feb 22, 2015 5:45 am

DUTOIT wrote:Use functions. With return value. That way you only code once and can call/use that function(code) all over the place.


I thought the problem with functions in this case is they don't continually run. so where i want the layers to fade into one another, a function cant do that (am i wrong) because it just 'starts' to set the opacity of the layer, then stops after the first tick.

Hoping for some genius wisdom to cause a paradigm shift in my thinking tho. Ive tried functions and just couldnt get it working as i need here.
Made Cosmochoria - www.cosmochoria.com
Currently working on Slayaway Camp - www.slayawaycamp.com
B
25
S
7
G
3
Posts: 375
Reputation: 4,852

Post » Sun Feb 22, 2015 6:25 am

Functions are used for repetitive code. You've got three sets of events doing exactly the same thing except the name of the layer being checked. Convert one of those blocks to be inside a function, using Function.Param(0) as the name to be checked, and replace the current blocks with a function call.

(You can't use Trigger once in this case, but that's a small loss, and not critical in this case.)
You do not have the required permissions to view the files attached to this post.
ImageImageImage
B
71
S
20
G
191
Posts: 3,535
Reputation: 108,844

Post » Sun Feb 22, 2015 6:38 am

You did it. you blew my mind. i never once thought i could/should call a function every tick like that. seems dumb now that i realize it... its doing what i was already doing every tick...

thanks man.
Made Cosmochoria - www.cosmochoria.com
Currently working on Slayaway Camp - www.slayawaycamp.com
B
25
S
7
G
3
Posts: 375
Reputation: 4,852

Post » Sun Feb 22, 2015 6:59 am

I'm sure you've used Functіons in your code before? :shock: Anyway the repetitive checking of 'activeLayer' had me twitching, checking the same thing 4 times in a row, so here's a slight mod of blackhornet's mod. Also, why set the red/blue/green in events instead of in properties? I guess because it's just a sample capx, but anyway I changed that too. I'm not a fan of using UID directly like that.
HoverOnLayers_BHT195.capx
You do not have the required permissions to view the files attached to this post.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Sun Feb 22, 2015 7:49 am

Yeah i use functions all over the place, but it was always for triggered stuff like.... hard to explain... like 'doing stuff on an action' type stuff. i didnt really think to use them in situations where they're checked every tick.. for some reason i thought that would be inefficient haha. im a lil embarrassed by it, of course. ;)
Made Cosmochoria - www.cosmochoria.com
Currently working on Slayaway Camp - www.slayawaycamp.com
B
25
S
7
G
3
Posts: 375
Reputation: 4,852

Post » Sun Feb 22, 2015 8:21 am

80bit wrote:I thought the problem with functions in this case is they don't continually run. so where i want the layers to fade into one another, a function cant do that (am i wrong) because it just 'starts' to set the opacity of the layer, then stops after the first tick.

Hoping for some genius wisdom to cause a paradigm shift in my thinking tho. Ive tried functions and just couldnt get it working as i need here.


Wow, see you got some great stuff from the others ;)

Yes, functions can continually run. As long as you give it a reason to run.
Call it everytick works or you can give it a reason to run
Just as example
run function = 1
if run fuction = 1
and x<100
do this or that
else
run function = 0 x =99
or whatever floats your boat.

Remember every tick is reading your eventsheets from top to bottom and if a condition isn't met the function/code will run until that condition is met.
You think you can do these things, but you can't, Nemo!
Just keep reading.
Just keep learning.
B
65
S
16
G
9
Posts: 1,429
Reputation: 12,698

Post » Sun Feb 22, 2015 6:08 pm

time to clean up haha
Made Cosmochoria - www.cosmochoria.com
Currently working on Slayaway Camp - www.slayawaycamp.com
B
25
S
7
G
3
Posts: 375
Reputation: 4,852


Return to How do I....?

Who is online

Users browsing this forum: Cryttexx and 2 guests