BLOGGER TEMPLATES AND TWITTER BACKGROUNDS

Sunday, 3 January 2010

Describing bits of the code

Here is a small example of the code i built explaining how I achieved the side scrolling. mcDevil is the character movie clip. charSpeed is the pixels per frame that the character moves in either direction (depending on which arrow key is pressed). wall2 is a wall background graphic. The following code is accessed on every frame to check the position of the character and make sure it can only move between 100px and 400px on the x axies. If it does try going past those positions the character is moved in the opposite direction with the same pixels per frame (charSpeed)therefore not moving anyway. This also triggers the background to move instead so instead of the character moving along the level, the level is actually moving behind the character.

stage.addEventListener(Event.ENTER_FRAME, bgScroll);

function bgScroll(event:Event):void{

if (mcDevil.x >= 400){
mcDevil.x -= charSpeed;
wall2.x -= charSpeed;
}

if (mcDevil.x <= 100){
mcDevil.x += charSpeed;
wall2.x += charSpeed;
}


To scroll the background for an infinite amount of time I made it so the scrolling movieclips such as the grass were 2 identical backgrounds next to each other. Then when the entire movie clip has moved the length of one, the entire clip moves the length of 1 of them, so it seems that the background scrolls seemingly seemlessly. Here is an extract from the code demonstrate.

if (bg1.x <= -1){
bg1.x = bg1.width/2;
}
if (bg1.x >= bg1.width/2 +1){
bg1.x = 0;
}


Here shows how I made the character flip directions according to which direction he is moving, which is triggered by the value of the boolean faceRight

if (faceRight == true){
mcDevil.scaleX = 1;
}
if (faceRight == false){
mcDevil.scaleX = -1;
}

0 comments: