When the world is becoming bigger, we need to either scroll the world or divide it into smaller parts. We will focus on scrolling the world in this chapter and discuss about dividing the world in next chapter.
We will add a function in World
class now. The lookAtAvatar
function offset the world position to have the avatar locate at the center of the screen.
public function lookAtAvatar(avatar:Avatar):void { /* the center of the screen */ var targetScreenPoint:Point = new Point(stage.stageWidth/2,stage. stageHeight/2); var localTargetPoint:Point = this.globalToLocal( targetScreenPoint); this.x += localTargetPoint.x - avatar.x; this.y += localTargetPoint.y - avatar.y; }
In the walking
function in WalkingAvatar
class, we can keep putting the controlling avatar in the center of the screen.
if (this == world.myAvatar){ world.lookAtAvatar(this); }
Another common scrolling method is to scroll the world when the avatar is walking into the edge of the screen. We change the preceding...