Home Lessons Apps Support About Consultations We create games Contact
Home Lessons Apps Support About Consultations We create games Facebook

Lesson 12: Subordinate characters.

Select the device which you develop the application for:
 

The following issues are shown in the project:
- a character that works by timer
- a character that manages subordinate characters;
- a composite character;
- output of variable values and of text messages.
 

Start Game Pilot, select "Prepare demo project", then select the "12 Subordinate characters" project. The project will load to your device into the Documents/Lesson012 folder.

The project consists of the scene Start which has the characters shown on the picture below:

The scene is described in the file Lesson012.sc:

 

- - - - - CODE BEGIN - - - - -

// Lesson 12. Subordinate characters
Scene Start
Item = 1+Random(10) // Making a subject from 1 to 10
CountItems = 1+Random( 5) // Making a number from 1 to 5
Png(Background,,,ScreenX,ScreenY) // The background for the game
Person(heart, 1,ScreenCX, 55*TypeDevice) // The heart beats on the timer
Person(Items, 1,100*TypeDevice,170*TypeDevice) // An example of a composite character
Person(meneger,1,ScreenX-100*TypeDevice, 170*TypeDevice) // Example of subordinate characters
// The numbers and quantities of items
Value(Item ,ScreenCX, 180+100*TypeDevice, ColorRed, 50*TypeDevice,TimesNew)
Value(CountItems,ScreenCX, 100+100*TypeDevice, ColorRed, 50*TypeDevice,TimesNew)

- - - - - CODE END - - - - -

 

Note that this program can be executed on iPad as well as on iPhone. For placing the elements on the screen depending on a device, the coordinates are calculated using the following variables:

 
Variable Description Value
TypeDevice
 
Device Type 1 - iPhone; 2 - iPad
ScreenX
 
Screen width at the current orientation One of the following values: 320; 480; 568; 768; 1024
ScreenY
 
Screen height at the current orientation One of the following values: 320; 480; 568; 768; 1024
ScreenCX
 
X coordinate of the screen center One of the following values: 160; 240; 284; 384; 512
ScreenCY
 
Y coordinate of the screen center One of the following values: 160; 240; 284; 384; 512

The description of the heart character is in the file heart.stt:

 

- - - - - CODE BEGIN - - - - -

 // heart.stt - An example of operation in timer
 Scale=10
 Img=1                 // Image heart001.png
 Slot[Time0_heart]=2   // The timer event is associated with state 2
 Rest

1: Timer[0]=1000       // The period of the timer is 1 sec.
   Rest

2: Delay = 200 dS = 10 // The heart increases gradually
   dS = -10            // The heart decreases gradually
   Rest

- - - - - CODE END - - - - -

 

The character contains one picture heart001.png, that is loaded with the command Img=1 during the initialization (in the head of the file). The character works by timer. The timer event is connected with the state 2. In the state 2, the work of the character described: smooth decreasing and increasing.

There are several identical heart characters on the screen at the same time. They all work according to the same common message Time0_heart. This message makes the timer of that heart character, which is called from Lesson012.sc. The timer starts in the 1 state using the command Timer[0]=1000. After starting, the timer creates message Time0_heart every 1000 ms. The timer works as long as the character exists. If necessary, you can stop the timer with the command Timer[0].

Every character can be called from another character as a subordinate one. In our project, you can call the heart character from the meneger character. In this case, the heart character will be subordinate towards the meneger character.

Let us look into the description of the meneger character in the file meneger.stt:

 

- - - - - CODE BEGIN - - - - -

 // meneger.stt - Example of subordinate characters
 Scale=10+20*TypeDevice 
 Img=0                  // Image meneger000.png
 Slot[Touch_heart] = 3  // When you touch the heart, go to state 3
 Rest

1: Delay=0
   // Creating the desired number of subordinate characters
   i=1 to CountItems-1 { Person[i]=heart State[i]=0 Scale[i]=20 }
   State=Count+11    // Property Count - the number of child characters
   Rest

3: if Count=5
    i=Count downto 1 Destroy[i] // Cycle removal of all the child characters
   State=Count+11 // Now we turn to the placement status of the subordinate 
                     characters depending on the number of items

// The following states increase the number of child characters to 1 each other
   // 1 Item:
11:Person[1]=heart X[1]=200  Y[1]=200   State[1]=0 Scale[1]=20
   Rest
   // 2 Items:
12:                X[1]=  0  Y[1]=  0
   Person[2]=heart X[2]=400  Y[2]=400   State[2]=0 Scale[2]=20
   Rest
   // 3 Items:
13:                X[1]=  0  Y[1]=  0
                   X[2]=400  Y[2]=  0
   Person[3]=heart X[3]=400  Y[3]=400   State[3]=0 Scale[3]=20
   Rest
   // 4 Items:
14:                X[1]=  0  Y[1]=  0
                   X[2]=400  Y[2]=400
                   X[3]=400  Y[3]=  0
   Person[4]=heart X[4]=  0  Y[4]=400   State[4]=0 Scale[4]=20
   Rest
   // 5 Items:
15:                X[1]=  0  Y[1]=  0
                   X[2]=200  Y[2]=200
                   X[3]=400  Y[3]=400
                   X[4]=  0  Y[4]=400
   Person[5]=heart X[5]=400  Y[5]=  0   State[5]=0 Scale[5]=20
   Rest

- - - - - CODE END - - - - -

 

The character contains one picture meneger000.png, which is loaded using the command Img=1, during the initialization (in the head of the file). The command Slot[Touch_heart]=3 connects the touch message of the heart character with the state 3. In this state, the amount of objects increases and they re-organize on the screen. State 3 begins with the checking the amount of subordinate characters: if Count=5. If the amount of subordinate characters is equal to 5, all the child characters will be deleted using the command:

i=Count Downto 1 Destroy[i]

where Count is a property that determines the amount of subordinate characters.

In this loop, the characters are deleted, beginning from the last element up to the first element. You can make it differently and delete the elements beginning from the first one, like this:

i=1 to 5 Destroy[1]

While the command Destroy[i] is executing, the first character is deleted and its place takes the second one. In other words, the numbers of all characters are reduced by one. Therefore, the subordinate character number 1 should be destroyed 5 times.

If the order of deleting the character doesn't matter, then the first version of the loop is preferable.

Subordinate characters are created in the following loop:

i=1 to CountItems-1 { Person[i]=heart State[i]=0 Scale[i]=20 }

where:
i – is the loop variable;
CountItems – defined number of subordinate characters;
Person[i]=heart – command that creates the subordinate character heart;
State[i]=0 – command that makes the subordinate character go to the execution mode;
Scale[i]=20 – assigning the scale of the subordinate character, in %.

This loop creates one character less than needed, and then the amount of objects will be increased by 1.

Depending on the amount of objects, they are placed differently on the screen. For each amount of objects, there is a certain state for positioning them (states 11-15). The jump to the appropriate state is made by the command State=Count+11.

To compare work of the subordinate and composite characters, let us look into the description of the Items character in the Items.stt file:

 

- - - - - CODE BEGIN - - - - -

 // Items - An example of a composite character
 Scale=10+20*TypeDevice // The scale of a composite character
 Img=0                  // Square
 Slot[Touch_heart] = 3  // When you touch the heart, go to state 3
 FontSize=50            // The font size of the labels

1: Delay=0
   i=1 to 7 Parent[i]=0 // Prepare 7 children sprites
   Slot[Touch_Self]=2   // When you touch the subject, go to state 2
   New  =4 New=5        // The output labels
   State=CountItems+10  // Now we turn to the placement status of the sprites 
                           depending on the number of items

// Changing the item
2: Slot[Touch_Self] Mp3=1 dS=3 // Increasing by 3%
   Item = Item+1        // The number of the next premera
   if Item>10 Item=1
   New=4                // The conclusion of the item name
   Delay=200 dS=-3      // Reducing by 3%
   Delay=100 I=1 To 5 Img[I]=Item // Changing pictures
   Slot[Touch_Self]=2
   Rest

// Increasing the number of items
3: CountItems = CountItems + 1
   if CountItems>5 CountItems=1
   New=5                // Conclusion the number of items
   State=CountItems+10  // Now we turn to the placement status of the sprites 
                           depending on the number of items

// The conclusion of the item name from a file Localize.str
4: Text[6]=Item Color[6]=ColorBlue X[6]=200 Y[6]=500
   Rest

// Conclusion the number of items
5: Text[7]=11   Color[7]=ColorBlue X[7]=200 Y[7]=-150
   Rest

   // 1 Item:
11:Img[1]=Item Opacity[1]=100 X[1]=200     Y[1]=200
   I=2 To 5 Opacity[I]=0
   Rest
   // 2 items:
12:Img[1]=Item Opacity[1]=100 X[1]=  0     Y[1]=  0
   Img[2]=Item Opacity[2]=100 X[2]=400     Y[2]=400
   I=3 To 5 Opacity[I]=0
   Rest
   // 3 items:
13:Img[1]=Item Opacity[1]=100 X[1]=  0     Y[1]=  0
   Img[2]=Item Opacity[2]=100 X[2]=400     Y[2]=  0
   Img[3]=Item Opacity[3]=100 X[3]=400     Y[3]=400
   I=4 To 5 Opacity[I]=0
   Rest
   // 4 items:
14:Img[1]=Item Opacity[1]=100 X[1]=  0     Y[1]=  0
   Img[2]=Item Opacity[2]=100 X[2]=400     Y[2]=400
   Img[3]=Item Opacity[3]=100 X[3]=400     Y[3]=  0
   Img[4]=Item Opacity[4]=100 X[4]=  0     Y[4]=400
   Opacity[5]=0
   Rest
   // 5 items:
15:Img[1]=Item Opacity[1]=100 X[1]=  0     Y[1]=  0
   Img[2]=Item Opacity[2]=100 X[2]=200     Y[2]=200
   Img[3]=Item Opacity[3]=100 X[3]=400     Y[3]=400
   Img[4]=Item Opacity[4]=100 X[4]=  0     Y[4]=400
   Img[5]=Item Opacity[5]=100 X[5]=400     Y[5]=  0
   Rest

- - - - - CODE END - - - - -

 

The main feature of the composite character is that during the initialization, there are 7 child sprites created in the loop:

i=1 to 7 Parent[i]=0

The parent of all child sprites is the character itself (0). The first five sprites are used to output objects, sprite 6 is used to output names of the objects, and sprite 7 is used to output the amount of the objects. To output these texts, the states 4 and 5 are used. The text output is made by the command:

Text[i]=f

where i – number of the child sprite
f – number of the phrase from the Localize.str file.

For example, the Localize.str file contains the following text:

 

- - - - - CODE BEGIN - - - - -

0 |en       |ru      |
1 |Apple    |Яблоко  |
2 |Ball     |Мяч     |
3 |Balloon  |Шарик   |
4 |Shoes    |Ботинок |
5 |Bucket   |Ведро   |
6 |Butterfly|Бабочка |
7 |Carrot   |Морковь |
8 |Cup      |Чашка   |
9 |Mushroom |Гриб    |
10|Cake     |Пирожное|
11|Count `CountItems |Количество `CountItems|

- - - - - CODE END - - - - -

 

Each string of the file must begin with the phrase number. Texts of the phrases are separated by "|". Displayed text can include a variable value. To substitute a variable with a value, use the symbol "`" before the variable name. The first string must begin with the 0 number and contain codes of the used languages.
 

- Download the archive with project files for this lesson -


Privacy Policy    Terms of Use     Copyright © Rais Garifullin