Vcam(what am I doing wrong?)
I'm getting this weird occurrence where my Vcam is lagging one frame behind. I'm working on a platformer, and its really annoying. I get no error code or anything, but the Vcam displays the scene from the previous frame. It's especially noticeable when the HUD jiggles around. As the player moves, the vcam moves, and the hud moves to a point relative to the vcam, but since the vcam displays an outdated view, the HUD moves around instead of staying stationary. Here's my code:
Code:
//declare initial variables
var menu = new GameMenu();
var level = new Level1();
var player = new Player();
var Enemies = new MovieClip();
var Coins = new MovieClip();
var vcam = new Vcam();
addChild(vcam);
vcam.x = 275;
vcam.y = 200;
var hud = new Hud();
var deco = new Deco1();
var CoinList = new Array();
var EnemyList = new Array();
var left = 37;
var up = 38;
var right = 39;
var down = 40;
var Keys = new Array();
for(var i=0;i<255;i++){
Keys.push(false);
}
var score = 0;
var health = 3;
var editor = false;
var leveln = 0;
//initialize menu
initialize(0);
//array of coin data
var AddCoins = new Array(
[[73,317],[122,307],[238,298],[387,286],[487,259],
[603,139],[701,67],[817,35],[906,99],[1039,42],
[992,335],[1249,-59],[1289,-58],[1337,8],[1417,63],
[1337,131],[1417,194],[1337,275],[1981,233],[2062,233],
[2130,233],[2040,352],[2101,352],[3016,351],[2845,-600],
[2845,-700],[2845,-800],[2845,-900],[2845,-1000],[2414,-978],
[2312,-843],[2263,-831],[2176,-758],[1858,-604]]
);
//array of enemy data
var AddEnemies = new Array(
[[603,136,1],[933.7,59.15,1],[1218.2,78.2,1],[1717.1,270.7,1],[2104.85,232.6,1],
[2517.55,350.9,1],[2426.4,-979.45,1],[2282.15,-842.1,1]]);
var Origin = new Array([282,289]);
//add function listeners
stage.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseup);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mousemove);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keydown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyup);
//main game function
function enterframe(e){
//if player is hurt, flash
if(player.hit > 1){
player.hit -= 1;
player.alpha -= 0.25;
if(player.alpha == 0){
player.alpha = 1;
}
}else if(player.hit == 1){
player.alpha = 1;
player.hit -= 1;
}
//control x axis
if(isKeyDown(left)){
if(player.sx > -player.maxx){
player.sx -= 1;
}
}else if(isKeyDown(right)){
if(player.sx < player.maxx){
player.sx += 1;
}
}else if(player.sx > 0){
player.sx -= 1;
}else if(player.sx < 0){
player.sx += 1;
}
//move player
player.sx = Math.round(player.sx);
player.x += player.sx;
//adjust for collission
while(level.hitTestPoint(player.x-player.maxx+1,player.y,true)){
player.sx = 0;
player.x += 1;
}
while(level.hitTestPoint(player.x+player.maxx-1,player.y,true)){
player.sx = 0;
player.x -= 1;
}
//control y axis
if(level.hitTestPoint(player.x,player.y+player.maxy,true)){
if(isKeyDown(up)){
if(deco.t1.hitTestPoint(player.x,player.y,true)){
player.sy = -player.maxy*3;
}else{
player.sy = -player.maxy;
}
player.y -= 1;
}
}else if(player.sy < player.maxy){
player.sy += 1;
}
//move player vertically
player.sy = Math.round(player.sy);
player.y += player.sy;
//adjust for collissions
while(level.hitTestPoint(player.x,player.y+player.maxy-1,true)){
player.sy = 0;
player.y -= 1;
}
while(level.hitTestPoint(player.x,player.y-player.maxy+1,true)){
player.sy = 0;
player.y += 1;
}
//move vcam if need be
while(vcam.x > player.x + 50){
vcam.x -= 1;
}
while(vcam.x < player.x - 50){
vcam.x += 1;
}
while(vcam.y > player.y + 50){
vcam.y -= 1;
}
while(vcam.y < player.y - 50){
vcam.y += 1;
}
if(vcam.x < 275){
vcam.x = 275;
}
if(vcam.y > 200){
vcam.y = 200;
}
//put hud in top left hand corner
hud.x = vcam.x - 270;
hud.y = vcam.y - 195;
//odds and ends
if(deco.t1.hitTestPoint(player.x,player.y,true)){
deco.jump.alpha = 1;
}else{
deco.jump.alpha = 0;
}
//allow coins to be picked up when not in editor mode
if(editor == false){
checkCoins();
}
//update enemies
updateEnemies();
}
//initialization function
function initialize(L){
if(L == 0){
//menu function
menu.alpha = 1;
stage.addEventListener(MouseEvent.MOUSE_DOWN,menuclick);
menu.x = 275;
menu.y = 143;
}else{
//level function
addChild(menu);
addChild(level);
addChild(player);
addChild(Enemies);
addChild(Coins);
addChild(hud);
addChild(deco);
player.sx = 0;
player.sy = 0;
player.hit = 0;
player.maxx = player.width/2;
player.maxy = player.height/2;
vcam.x = 275;
vcam.y = 200;
deco.jump.alpha = 0;
deco.t1.alpha = 0;
leveln = L;
for(var i=0;i<AddCoins.length;i++){
addCoin(new Coin(),AddCoins[leveln-1][i][0],AddCoins[leveln-1][i][1]);
}
for(i=0;i<AddEnemies.length;i++){
addEnemy(new Enemy(),AddEnemies[leveln-1][i][0],AddEnemies[leveln-1][i][1],AddEnemies[leveln-1][i][2]);
}
hud.x = vcam.x - 275;
hud.y = vcam.y - 200;
player.x = Origin[leveln-1][0];
player.y = Origin[leveln-1][1];
updateHud();
stage.addEventListener(Event.ENTER_FRAME,enterframe);
}
}
function terminate(L){
if(L == 0){
menu.alpha = 0;
stage.removeEventListener(MouseEvent.MOUSE_DOWN,menuclick);
}
//incomplete, no need for termination of game as of yet
}
function addCoin(O,X,Y){
Coins.addChild(O);
CoinList.push(O);
O.x = X;
O.y = Y;
if(editor == true){
O.num.text = CoinList.length;
}
}
function addEnemy(O,X,Y,T){
Enemies.addChild(O);
EnemyList.push(O);
O.x = X;
O.y = Y;
O.sx = Math.round(Math.random()*5)+1;
O.sy = 0;
//O.gotoAndStop(T);
if(editor == true){
O.num.text = EnemyList.length;
}
}
function checkCoins(){
for(var i=0;i<CoinList.length;i++){
if(CoinList[i].hitTestObject(player)){
Coins.removeChild(CoinList[i]);
CoinList.splice(i,1);
i -= 1;
score += 1;
updateHud();
}
}
}
function updateEnemies(){
for(var i=0;i<EnemyList.length;i++){
if(Math.abs(EnemyList[i].x - vcam.x) < 300 && Math.abs(EnemyList[i].y-vcam.y) < 225){
if(level.hitTestPoint(EnemyList[i].x,EnemyList[i].y+EnemyList[i].height/2,true)){
if(EnemyList[i].x > player.x && EnemyList[i].sx > 0){
EnemyList[i].sx = -EnemyList[i].sx;
EnemyList[i].scaleX = -1;
}
if(EnemyList[i].x < player.x && EnemyList[i].sx < 0){
EnemyList[i].sx = -EnemyList[i].sx;
EnemyList[i].scaleX = 1;
}
}
EnemyList[i].x += EnemyList[i].sx;
while(level.hitTestPoint(EnemyList[i].x-EnemyList[i].width/2+1,EnemyList[i].y,true)){
EnemyList[i].xs = 0;
EnemyList[i].x += 1;
}
while(level.hitTestPoint(EnemyList[i].x+EnemyList[i].width/2-1,EnemyList[i].y,true)){
EnemyList[i].xs = 0;
EnemyList[i].x -= 1;
}
if(level.hitTestPoint(EnemyList[i].x,EnemyList[i].y+EnemyList[i].height/2,true)){
if(EnemyList[i].y > player.y && Math.abs(EnemyList[i].x-player.x) < 100){
EnemyList[i].sy = -EnemyList[i].height/2;
EnemyList[i].y -= 1;
}
}else{
if(EnemyList[i].sy < EnemyList[i].height/2){
EnemyList[i].sy += 1;
}
}
EnemyList[i].y += EnemyList[i].sy;
while(level.hitTestPoint(EnemyList[i].x,EnemyList[i].y+EnemyList[i].height/2-1,true)){
EnemyList[i].sy = 0;
EnemyList[i].y -= 1;
}
while(level.hitTestPoint(EnemyList[i].x,EnemyList[i].y-EnemyList[i].height/2+1,true)){
EnemyList[i].sy = 0;
EnemyList[i].y += 1;
}
if(EnemyList[i].hitTestObject(player)){
if(Math.abs(EnemyList[i].x-player.x)<EnemyList[i].y-player.y){
Enemies.removeChild(EnemyList[i]);
EnemyList.splice(i,1);
i -= 1;
}else if(player.hit == 0){
health -= 1;
updateHud();
player.hit = 20;
player.alpha = 0.5;
}
}
}else{
if(!level.hitTestPoint(EnemyList[i].x,EnemyList[i].y+EnemyList[i].height/2,true)){
if(EnemyList[i].sy < EnemyList[i].height/2){
EnemyList[i].sy += 1;
}
EnemyList[i].y += EnemyList[i].sy;
while(level.hitTestPoint(EnemyList[i].x,EnemyList[i].y+EnemyList[i].height/2-1,true)){
EnemyList[i].y -= 1;
EnemyList[i].sy = 0;
}
}
}
}
}
function updateHud(){
var tens = 0;
var ones = score;
while(ones > 9){
ones -= 10;
tens += 1;
}
//hud.num1.gotoAndStop(tens+1);
//hud.num2.gotoAndStop(ones+1);
tens = 0;
ones = health;
while(ones > 9){
ones -= 10;
tens += 1;
}
//hud.num3.gotoAndStop(tens+1);
//hud.num4.gotoAndStop(ones+1);
}
function mousedown(e){
if(editor == true){
addEnemy(new Enemy(),mouseX,mouseY,1);
trace("addEnemy(new Enemy(),"+mouseX+","+mouseY+","+1+");//"+EnemyList.length);
}
}
function menuclick(e){
if(menu.newgame.hitTestPoint(mouseX,mouseY,true)){
terminate(0);
initialize(1);
}
if(menu.loadgame.hitTestPoint(mouseX,mouseY,true)){
//terminate(0);
//load game and initialize
}
if(menu.instructions.hitTestPoint(mouseX,mouseY,true)){
//terminate(0);
//load instructions
}
if(menu.credits.hitTestPoint(mouseX,mouseY,true)){
//terminate(0);
//load credits
}
}
function mouseup(e){
}
function mousemove(e){
}
function keydown(e){
Keys[e.keyCode] = true;
if(editor == true){
if(e.keyCode == 32){
addCoin(new Coin(),player.x,player.y);
trace("addCoin(new Coin(),"+player.x+","+player.y+");//"+CoinList.length);
}
}
}
function keyup(e){
Keys[e.keyCode] = false;
}
function isKeyDown(n){
return Keys[n];
}
Can anyone help me? I've run into the problem before and the fix was to remove a "gotoAndStop()" line. However, I tried commenting out every gotoAndStop to no avail. Thanks for the help, and good luck sorting through my very unorganized and unprofessional script.