Creating Function

3

Attached Files

The following files have been attached to this tutorial:

.capx

Stats

8,526 visits, 12,675 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 23 Jan, 2014. Last updated 19 Feb, 2019

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

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

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.

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

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:

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

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

.CAPX
  • 0 Comments

Want to leave a comment? Login or Register an account!