Thursday, October 25, 2012

Joe Willmott's Blog: Flash AS3 camera to follow your character

For years I have used Sham Banghal's vCam in my flash games. It has always done what I needed of it and it was easy to use, however my latest game is a big one and as a result, it creates a decent amount of lag. I have looked into it and the lag was caused by all of the complex vector graphics. A way around this was to make a new camera that only rendered the graphics that are currently on-screen, saving the computer from having to render everything all of the time. I used scrollRect to create the simplest of cameras and would like to share it with anyone else looking for a very simple camera to use in their flash games! The only code required is this:
import flash.events.Event; import flash.geom.Rectangle; stage.addEventListener(Event.ENTER_FRAME, cameraFollowCharacter); function cameraFollowCharacter(evt:Event){ root.scrollRect = new Rectangle(char.x - stage.stageWidth/2, char.y - stage.stageHeight/2, stage.stageWidth, stage.stageHeight); }
As you can see from the code, you will also need a movie clip on-stage called "char" (or if it is called something else, change the bits that say "char" in the code. This is an incredibly simple camera but very effective as it completely removed the lag from my game! I have also attached an example .fla so you can see how it all works (saved in Flash CS4): Source file: Joe Willmott's ActionScript 3.0 Camera

read more: Joe Willmott's Blog: Flash AS3 camera to follow your character

No comments:

Post a Comment