How to implement Color Infection rules from scratch

From Color Infection Wiki
Jump to: navigation, search

In this tutorial, we will implement the classic Color Infection rules from scratch.

Firstly, let's view an example with the Enable Color Infection Rules option of the world is checked:

Now, let's implement the classic Color Infection rules from scratch. In addition, we will also make some small appearance modifications on the classic color infection levels:

  1. chage infected color from brown to red;
  2. chage uninfected color from yellow to white;
  3. chage "don't infected" color from gree to light blue;
  4. chage breakable color from pink to black.

(NOTICE: you can select the "Copy Source Code" from the context menu of all flash windows in this tutorial and load the copied source code in Phyard Builder.)

Create The Level Scene

  • Create a level with default world settings.
  • Uncheck the Enable Color Infection Rules option in Config Level Rules Dialog.
  • Create the level scene according the above example with mentioned appearance modifications.

then we get the following half-finished design:

Implement Breakable Shapes

  • Create An Entity Input Entity Selector and make it selecting all the 4 black breakable shapes.
  • Create A On Shape Mouse Click Event Handler and link it with the just created Entity Input Entity Selector. This means this event handler will only the mouse click events occur on the black breakable shapes.
  • Open the setting dialog of the just created On Shape Mouse Click Event Handler, then begin coding.
    • add a Destroy Entity core API calling.
    • select the input variable The Shape as the only input parameter.

Now, when you click on any of the 4 black shapes, they will be destroyed:

Store Color Values In Global Variables

  • Make a clone for each of the 3 balls with red color, white color and light blue color. Then open their setting dialog and uncheck the 'Enable Physics option.
  • Create an On Level Begin Initializing Event Handler.
  • Open its setting dialog and create 3 number global variables: Color Infected, Color Not Infected and Color Don't Infect.
  • Initialize the 3 global variables with the background/filled color of the just cloned 3 balls.

The 3 global variables are also visible in other event handlers which will be created soon.

Implement Color Infecting

  • Create an Entity Pair Selector. Select it then right click on it to show its context menu, change its pairing type to Any To Any.
  • Create an On 2 Physics Shapes Start Contacting Event Handler, and link it with the just created Entity Pair Selector. This means this event handler will handler all 2 Physics Shapes Start Contacting events.
  • Open the setting dialog of the just created On 2 Physics Shapes Start Contacting Event Handler and begin coding.
    • first retrieve the background colors of the 2 contacting shapes. If neither the 2 colors is the Infected Color, then there is no needs to continue.
    • create a global varuable Has Shapes Infected Wrongly with initial value false.
    • for shape 1:
      • if the background color of shape 1 is Not Infected Color, change it to the Infected Color.
      • if the background color of shape 1 is Don't Infected Color, also change it to the Infected Color, and set the global varuable Has Shapes Infected Wrongly with value true.
    • sames for shape 2

Now colors can infect:

Check Whether Or Not Level Is Finished

  • Create an On Before Level Initializing Event Handler, open its setting dialog,
    • add a global variable Number Of Not Infected Shapes
    • reset this variable with value 0.
  • Create an Entity Selector. Select it then right click on it to show its context menu, change its type to Any.
  • Create an On Entity Updated Event Handler, and link it with the just created Entity Selector. This means this event handler will handler all On Entity Updated events.
  • Open the setting dialog of the just created On 2 Physics Shapes Start Contacting Event Handler and begin coding.
    • check whether the input entity is a physics enabled shape or not. If not, no needs to continue.
    • if the color the physics enabled shape is Not Infected Color, increase the value of global variable Number Of Not Infected Shapes by one.
  • Create an One After Level Updated Event Handler, open its setting dialog and code:
    • if the value of global variable Has Shapes Infected Wrongly is true, then set level status as Failed;
    • if the value of global variable Has Shapes Infected Wrongly is false
      • if the value of global variable Number Of Not Infected Shapes is 0, set level status as Succeeded;
      • if the value of global variable Number Of Not Infected Shapes is not 0, set level status as Unfinished;

Done! The final level: