Copy link to clipboard
Copied
Im currently working on an incorporation of a couple of different projects adapted to this one. Pretty much what i want to happen is for my "hero" sprite to shoot an instance of sword out of his body.
unfortunatly im geting this error
Line 153 | 1061: Call to a possibly undefined method push through a reference with static type Class. |
Ive searched the net to find something similar but in the last 40 minutes before i got to sleep i havent had success.
Level1 code (main script)
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;
import flash.utils.getTimer;
public class level1 extends MovieClip
{
private var scenery:Array; // walls, floors etc
private var otherObjects:Array; // key, door etc
private var hero:Object;
private var enemies:Array;
private var inventory:Array; // objects the player has picked up
private var gameMode:String = "start";
private var lives:int;
private var lastTime:Number = 0; // time of last frame
private var gotKey:Boolean;
private var sword:Array;
public function startMainGame()
{
inventory = new Array();
gameMode = "play";
lives = 3;
}
public function startLevel()
{
// link variables to the various game objects
getHero();
getEnemies();
examineLevel();
// add listeners
this.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN,
keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
// set up variables
gameMode = "play";
gotKey = false;
}
public function getHero()
{
hero = new Object();
hero.mc = gameLevel.hero; // link to the movie-clip
// set initial properties
hero.dx = 0;
hero.dy = 0;
hero.direction = 0; // which way are we facing (1 = right)
// track which actions are true, based on key-presses
hero.moveLeft = false;
hero.moveRight = false;
hero.moveUp = false;
hero.moveDown = false;
hero.collision = false;
hero.gotKey = false;
hero.width = gameLevel.hero.width + 5;
hero.height = gameLevel.hero.height + 5;
hero.walkSpeed = 0.15;
// record starting position to reset to if player dies
hero.startx = gameLevel.hero.x;
hero.starty = gameLevel.hero.y;
trace("get hero complete ");
}
public function getEnemies()
{
trace("get hero completed ");
enemies = new Array();
var enemyCount:int = 1;
while (gameLevel["enemy"+enemyCount]!=null)
{
trace("Found enemy " + enemyCount);
var enemy = new Object();
enemy.mc = gameLevel["enemy"+enemyCount];
enemy.dx = 0.0;
enemy.dy = 0.0;
enemy.inAir = false;
enemy.direction = 1;
enemy.jump = false;
enemy.moveRight = true;
enemy.moveLeft = false;
enemy.jumpSpeed = 1.0;
enemy.walkSpeed = 0.08;
enemy.width = enemy.mc.width;
enemy.height = enemy.mc.height;
// add to the array and increment count
enemies.push(enemy);
enemyCount++;
}
}
public function examineLevel()
{
scenery = new Array();
otherObjects = new Array();
// loop through all objects in gameLevel
for (var i:int=0; i<gameLevel.numChildren; i++)
{
var mc = gameLevel.getChildAt(i);
// floors or walls go in the scenery array
if ((mc is Wall) ||(mc is Floor))
{
var sceneryObject:Object = new Object();
sceneryObject.mc = mc;
sceneryObject.leftside = mc.x;
sceneryObject.rightside = mc.x + mc.width;
sceneryObject.topside = mc.y;
sceneryObject.bottomside = mc.y + mc.height;
scenery.push(sceneryObject);
}
// doors and keys go in the otherObjects array
else if ((mc is Key) || (mc is Gate))
{
otherObjects.push(mc);
}
}
trace("examing game completed ");
}
public function keyDownFunction(event:KeyboardEvent)
{
if (gameMode=="play") // can't move if we aren't in this mode
{
if (event.keyCode==37)
{
hero.moveLeft = true;
}
if (event.keyCode==38)
{
hero.moveUp = true;
}
if (event.keyCode==39)
{
hero.moveRight = true;
}
if (event.keyCode==40)
{
hero.moveDown = true;
}
if (event.keyCode==32)
{
// create a new bullet at the top of the gun
var b = new Sword(hero.x, hero.y, -300);
trace("Sword instantiated ");
addChild(b);
trace("Sword added to array ");
Sword.push(b)as MovieClip;
trace("Sword Created ");
}
}
}
public function keyUpFunction(event:KeyboardEvent)
{
if (event.keyCode==37)
{
hero.moveLeft = false;
}
if (event.keyCode==38)
{
hero.moveUp = false;
}
if (event.keyCode==39)
{
hero.moveRight = false;
}
if (event.keyCode==40)
{
hero.moveDown = false;
}
}
public function gameLoop(event:Event)
{
if (lastTime==0)
{
lastTime = getTimer();
}
var timeDiff:int = getTimer() - lastTime;
lastTime += timeDiff;
if (gameMode=="play")
{
moveCharacter(hero,timeDiff);
checkForCollisions(); }
}
public function moveCharacter(character:Object, elapsedTime:int)
{
if (elapsedTime>0) // don't apply calculations on the very first frame
{
// work out whcih way the character is facing
var oldDirection = character.rotation;
var horizontalSpeed:Number = 0;
var verticalSpeed:Number = 0;
if (character.moveRight)
{
//trace ("moveright");
character.rotation = 90;
verticalSpeed = 0;
horizontalSpeed = 1;
if (character.rotation!=oldDirection)
{
character.mc.rotation = 90;
}
}
else if (character.moveLeft)
{
//trace ("moveleft");
character.rotation = 270;
verticalSpeed = 0;
horizontalSpeed = -1;
if (character.rotation!=oldDirection)
{
character.mc.rotation = 270;
}
}
else if (character.moveUp)
{
//trace ("moveup");
character.rotation = 0;
horizontalSpeed = 0;
verticalSpeed = -1;
if (character.rotation!=oldDirection)
{
character.mc.rotation = 0;
}
}
else if (character.moveDown)
{
//trace ("movedown");
character.rotation = 180;
horizontalSpeed = 0;
verticalSpeed = 1;
if (character.rotation!=oldDirection)
{
character.mc.rotation = 180;
}
}
for (var i:int=0; i<scenery.length; i++)
{
if (hero.mc.hitTestObject(scenery.mc))
{
if (horizontalSpeed==1)
{
//trace("collision");
hero.colliosion = true;
horizontalSpeed==0;
break;
}
else if(horizontalSpeed==-1)
{
//trace("collision");
hero.colliosion = true;
horizontalSpeed==0;
break;
}
else if(verticalSpeed==-1)
{
//trace("collision");
verticalSpeed==0;
hero.colliosion = true;
}
else if(verticalSpeed==1)
{
verticalSpeed==0;
//trace("collision");
hero.colliosion = true;
}}}
if (hero.colliosion == true)
{
//trace("collision blocked");
hero.colliosion == false;
}
else{
var newY = character.mc.y + character.walkSpeed *
verticalSpeed * elapsedTime/2;
character.mc.y = newY;
var newX = character.mc.x + character.walkSpeed *
horizontalSpeed * elapsedTime/2;
character.mc.x = newX;
}
}
}
public function checkForCollisions()
{
//trace ("checking for collisions");// check for collisions between hero and enemy
for (var i:int=enemies.length-1; i>=0; i--)
{
if (hero.mc.hitTestObject(enemies.mc))
{
if ((hero.inAir) && (hero.dy>0)) // hero is falling
{
enemyDeath(i);
}
else
{
heroDeath();
return;
}
}
}
for (var i:int=otherObjects.length-1; i>=0; i--)
{
if (hero.mc.hitTestObject(otherObjects))
{
if (otherObjects is Key)
{
gotKey = true;
gameLevel.removeChild(otherObjects);
//trace("got key");
//otherObjects.splice(i,1);
}
else if (otherObjects is Gate)
{
//trace("hit gate");
if (gotKey==true)
{
//trace("clean up");
cleanUp();
nextFrame();
}
}
}
}
}
public function cleanUp()
{
}
public function heroDeath()
{
trace("player death");
}
public function enemyDeath(index:int)
{
trace("player death");
gameLevel.removeChild(enemies[index].mc);
enemies.splice(index,1);
}
}}
i know theres a couple of empty methods there, I am waiting to get the rest of my code working first before starting on them.
the Sword.as code :
package
{
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
public class Sword extends MovieClip
{
private var dy:Number; // speed of movement
private var lastTime:int; // used in controlling animation
public function Sword(x,y,speed:Number)
{
this.x = x;
this.y = y;
dy = speed;
// set up animation event
addEventListener(Event.ENTER_FRAME, moveSword);
lastTime = getTimer();
}
public function moveSword(event:Event)
{
// work out how long since the last frame
var thisTime:int = getTimer();
var timePassed:int = thisTime - lastTime;
lastTime = thisTime;
// now move the plane
y = y + (dy * timePassed / 1000);
// delete the clip if its off the top of the screen
if (timePassed > 10)
{
deleteSword();
}
}
// called if bullet goes off screen
public function deleteSword()
{
parent.removeChild(this);
removeEventListener(Event.ENTER_FRAME, moveSword);
}
}
}
Thanks for any help.
Im off to bed, been working on this for close to 14 hours and its 3 am 😕
Ill check back in a bout 4 hours when i get up.
Jake
EDIT: sorry i wasnt able to foram the code properly
Copy link to clipboard
Copied
the first push error is on this line which is quite a mess:
Sword.push(b)as MovieClip; |
1. Sword is a class name, not an array instance. you need fix that.
2. in addition, the push() method returns an uint (the array length), not a MovieClip so you need to fix that cast attempt.
3. (and, there's a forum artifact or a typo because there should be a space before "as".)
Find more inspiration, events, and resources on the new Adobe Community
Explore Now