Tutorial Downloads

File Size
Function.capx2.16MB

Translations

Know another language? Translate this tutorial!

Stats

3.8K visitors
14.7K page views

Creating Function

Favourite 16 favourites
Tutorial written by Lordshiva1948Originally published on 23rd, January 2014 - 1 revision

Function

What is function you may ask? What can function do? Well with function you create certain Task Utility that can be used again and again without writing same code. We can use function like death of alien in game or player and so on.

Let’s create a small basic game with function.
1. Create new project and call it Function for now
2. Set size to 1500 x 640
3. Set margin to 1500 x 640
4. Set windows size to 960 x 640

Once this is set then save it as Function

Now we create Background
1. Create sprite and name it Background1
2. Size 1080 x 640
3. Now set imagepoint 0 to 0 and 640
4. Add new Imagepoint 1 and set to 1080 and 640
5. Set behaviour to BULLET and set speed 250
6. Set angle to NO
7. Place this Background at 0,320
Now clone the Background1 this now will be Background2
Put this Background2 out of site

Please create sprite and name it Trigger1
NO BEHAVIOUR FOR THESE TRIGGERS
1. Set size to 16 x 660 any colour you like
Now place Trigger1 position to X=-16 and Y=320
Now Clone Trigger1
Place Trigger2 Position at X=-16 and Y=320

Please note Layers should be named like this Background1 and Background2 will be placed on Layer named Back.

Now open your event sheet and insert code below


Now add this code
Background
Open full size image

Run the layout you should see your background scrolling. Lock the layer Back and create another layer and name it Objects.
Your layers should be like this

Please lock other layers while you are working on Player layer
First let’s create Player and name it Hero
Now add TOUCH for controlling Hero and add this code
Open full size image

Please make sure you type in correctly or you will get strange result. Try it out. Run it and see if your HERO goes up and down only. I have included CAPX file also. Now the Hero is done. Locked the layer and unlock Object layer.
Create sprite and name it Coin
Set size 32 x 32
Behaviour to Bullet
Speed 250
Destroy outside layout
Set angle to NO

Once we added Coin now most important thing to add is FUNCTION so add Function

Once you done that now Create new Event sheet and name it Function
Go on to events and right CLICK mouse button. You will notice dialog box in this click on

Include event sheet. Another Dialog box opens

Here we have Function Event Sheet click ok. Now your project will look

like this:
Not much is it?
Before we go on to Function events please add these global Variables

Now that is done let’s go on to Function event Sheet and add these code: before we do that you have to give name for Function. Let’s say Create is our Function Name.
Now add this code carefully
Remembering to add Local Variable just like in my Code. I hope you know to how to or read C2 Manuals.
Open full size image

Now let’s test it but, before you can do that you have to call this Create Function from event sheet 1; to do that enter this code in Event Sheet 1
Open full size image

When you run it is be very patient your object coin will appear.
If you have type in properly you will see coins row: see CAPX
Now we want to add Enemies and Bonus stuff on Layer Enemies
Go and create enemy and same behaviour as Coin



Once you add these objects then go on to Function Sheet and add this code carefully or it will not work:
Open full size image

Now run it you will see snake, Bonus and Fly will randomly create on your screen.
Now we come to Collisions here you have to be very careful or result well what can one say?
To add score and so on you should be able to do that I am sure if not then read C2 tutorial please also see CAPX I have included.

Function Collision Hero Dead


Once you created Function Dead now let’s call that Function Dead from our Event Sheet.
Here I have called Enemy Collision
Open full size image

As you can see there is not much code in this is it?
Therefore FUNCTION is great to be used in your project. Now I have showed you how to create function there is no limit what one can create.
Example power up
Make screen speed up when power up is collected
Make hero scale larger when Hero Collision with Power up, also invincible for little while from Enemies. Then set game speed back to normal
Endless choice. I hope you like this tutorial

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

0
Pennington Games 3,087 rep

Thanks For the tips!

Thursday, January 23, 2014 at 10:01:20 AM
1
EddyDingDongs 3,407 rep

thanks dude, good explanation

Thursday, January 23, 2014 at 11:05:47 AM
0
AbelaNET 14.8k rep

Thanks for sharing

Sunday, January 26, 2014 at 12:34:15 PM
3
Lordshiva1948 41.0k rep

I have sent you email or shall I say private msg how to do all that

Monday, January 27, 2014 at 2:01:48 PM
0
chadorireborn 59.9k rep

Nice Tutorial @Lordshiva1948 . This is very helpful in explaining the "Call function()" and the "parameter" but can you explain and make an example of the use of the "set return value" and "Call expression". Thanks

Sunday, April 24, 2016 at 6:14:56 AM
0
goodwin88 206 rep

You write:
"Once we added Coin now most important thing to add is FUNCTION so add Function"

Then there is a small cropped image.

Then the very next paragraph starts with:
"Once you done that now Create new Event sheet and name it Function"

There is no description of how to add function. This is the most important thing and I do not see how to do this. The small cropped image does not help.

Friday, August 19, 2016 at 9:09:57 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.