Skip to content

C2D 04 Accessing Components

Robert Silverton edited this page Aug 12, 2013 · 8 revisions

Currently our scene is a little bit dull. Let’s add some animation to our scene by making our rectangle rotate.

Accessing Components screenshot

Add the following code to the start of your enterFrameHandler:

private function enterFrameHandler( event:Event ):void
{
	var rectangleEntity:ComponentContainer = ComponentUtil.getChildByName(cadetScene, "Rectangle");
	var transform:Transform2D = ComponentUtil.getChildOfType(rectangleEntity, Transform2D);
	transform.rotation += 1;
	var skin:GeometrySkin = ComponentUtil.getChildOfType(rectangleEntity, GeometrySkin);
	skin.fillAlpha = mouseX/stage.stageWidth; 
		
	cadetScene.step();
}

The first thing we’re doing is grabbing a reference to our rectangle Entity again. Obviously we could have simply retained a reference to this in the same way we’re storing a reference to our CadetScene, but this way we can see how to use some utility functions to get a reference to objects in our scene.

Remember that the structure of our scene currently looks like this:

  • CadetScene
    • RectangleEntity
      • Transform2D
      • ShapeSkin
      • Rectangle Geometry

So first we are getting our rectangleEntity back ‘byName’. Then we are grabbing a reference to the Transform2D Component we added to our ComponentContainer ‘byType’. We then simply increment the rotation property of the transform.

We also grab a reference to our Skin in the same way. Here we are setting the fillAlpha property of the skin so it is 100% when the mouse is on the right side of the screen, and 0% when on the left.

< Previous | Next >