I'm creating a game where if you hit a zombie the zombie dies and his head is sent back in the direction you hit. I have two movieclips the zombie and the zombie head. Once the zombie is hit it will play its dying animation and remove itself and at the same time the zombie head will be added to the dying zombie and be blown back. I have done the code for the hittest and the zombie dying and respawning but I can't seem to position and add the head to the dying zombie when it is hit. How can I do this. I thought it will be like this, I added this in the PlayDeathAnimation function in the zombie class but did not work:
for (var i=0; i < MovieClip(parent).zombies.length; ++i)
{
var zh = new ZombieHead();
zh.x = MovieClip(parent).zombies[i].x;
zh.y = MovieClip(parent).zombies[i].y;
zh.rotation = MovieClip(parent).zombies[i].rotation;
addChild(zh);
}
I have 4 classes
Player
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.display.Graphics;
import flash.utils.setTimeout;
public class Player extends MovieClip
{
//Player Setting
var walkSpeed:Number = 4;
var walkRight:Boolean = false;
var walkLeft:Boolean = false;
var walkUp:Boolean = false;
var walkDown:Boolean = false;
var attacking:Boolean = false;
var attackRange:int = 100;
var attackAngle:int = 30;
public function Player()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,walk);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,stopWalk);
stage.addEventListener(MouseEvent.CLICK,attack);
// The lines below draw a preview of your attack area
graphics.beginFill(0x00ff00, 0.2);
graphics.lineTo(attackRange*Math.cos((rotation-attackAngle)/180*Math.PI),attackRange*Math.sin((rotation-attackAngle)/180*Math.PI));
graphics.lineTo(attackRange*Math.cos((rotation+attackAngle)/180*Math.PI),attackRange*Math.sin((rotation+attackAngle)/180*Math.PI));
graphics.endFill();
}
function walk(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 68)
{
walkRight = true;
}
if (event.keyCode == 87)
{
walkUp = true;
}
if (event.keyCode == 65)
{
walkLeft = true;
}
if (event.keyCode == 83)
{
walkDown = true;
}
}
function Update(event:Event)
{
//if attacking is true then key moves are false;
if ((attacking == true))
{
walkRight = false;
walkLeft = false;
walkUp = false;
walkDown = false;
// see if the zombie is in the cone
for (var i:int=MovieClip(parent).zombies.length-1; i>=0; i--)
{
if (inAttackCone(MovieClip(parent).zombies[i]))
{
if (hitTestObject(MovieClip(parent).zombies[i]))
{
//attacking = true;
MovieClip(parent).zombies[i].zombieDead = true;
}
}
}
}
else if ((attacking == false))
{
//Else if attacking is false then move and rotate to mouse;
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
if ((walkRight == true))
{
x += walkSpeed;
gotoAndStop(2);
}
if ((walkUp == true))
{
y -= walkSpeed;
gotoAndStop(2);
}
if ((walkLeft == true))
{
x -= walkSpeed;
gotoAndStop(2);
}
if ((walkDown == true))
{
y += walkSpeed;
gotoAndStop(2);
}
}
}
//Calculate the distance between the two
public function distanceBetween(player:MovieClip,zombies:MovieClip):Number
{
for (var i:int=MovieClip(parent).zombies.length-1; i>=0; i--)
{
var dx:Number = x - MovieClip(parent).zombies[i].x;
var dy:Number = y - MovieClip(parent).zombies[i].y;
}
return Math.sqrt(((dx * dx) + dy * dy));
}
public function angleDifference(a:Object, b:Object):Number
{
var dx:Number = b.x - a.x;
var dy:Number = b.y - a.y;
var ang:Number = (a.rotation/180*Math.PI)-Math.atan2(dy, dx);
while (ang>Math.PI)
{
ang -= 2 * Math.PI;
}
while (ang<-Math.PI)
{
ang += 2 * Math.PI;
}
return Math.abs(ang*180/Math.PI);
}
function inAttackCone(enemy:MovieClip):Boolean
{
var distance:Number = distanceBetween(this,enemy);
distance -= enemy.width / 2;// account for the enemy's size
if (distance < attackRange)
{
// In range, check angle
if (angleDifference(this,enemy)<attackAngle)
{
return true;
}
}
return false;
}
function stopWalk(event:KeyboardEvent)
{
if ((attacking == false))
{
if (event.keyCode == 68)
{
event.keyCode = 0;
walkRight = false;
gotoAndStop(1);
}
if (event.keyCode == 87)
{
event.keyCode = 0;
walkUp = false;
gotoAndStop(1);
}
if (event.keyCode == 65)
{
event.keyCode = 0;
walkLeft = false;
gotoAndStop(1);
}
if (event.keyCode == 83)
{
event.keyCode = 0;
walkDown = false;
gotoAndStop(1);
}
}
}
function attack(event:MouseEvent)
{
if ((attacking == false))
{
attacking = true;
gotoAndStop(3);
}
}
}
}
Zombies
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
public class Zombie extends MovieClip
{
var walkSpeed:Number = 2;
var target:Point;
public var zombieDead:Boolean = false;
public function Zombie()
{
//set target location of Zombie
target = new Point(Math.random() * 500,Math.random() * 500);
}
function loop()
{
if (zombieDead == true)
{
playDeathAnimation();
}
else if (zombieDead == false)
{
gotoAndStop(1);
//Point Zombie at its target
var dx = MovieClip(root).Player01.x - x;
var dy = MovieClip(root).Player01.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
//Move in the direction the zombie is facing
x = x+Math.cos(rotation/180*Math.PI)*walkSpeed;
y = y+Math.sin(rotation/180*Math.PI)*walkSpeed;
//Calculate the distance to target
var hyp = Math.sqrt((dx*dx)+(dy*dy));
if (hyp<5)
{
target.x = Math.random() * 500;
target.y = Math.random() * 500;
}
}
}
public function playDeathAnimation()
{
gotoAndStop(2);
}
public function deathAnimationFinishedCallback()
{
for (var i=0; i < MovieClip(parent).zombies.length; ++i)
{
if (MovieClip(parent).zombies[i] == this)
{
MovieClip(parent).zombies.splice(i, 1);
break;
}
}
MovieClip(parent).removeChild(this);
}
}
}
Document Class
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class Game extends MovieClip
{
public var zombies:Array;
public function Game()
{
addEventListener(Event.ENTER_FRAME, update);
zombies = new Array();
}
public function update(e:Event)
{
//Only spawn a Zombie if there are less than number
if (numChildren < 4)
{
//Make a new instance of the Zombie.
var Z = new Zombie();
addChild(Z);
//Position and rotate the zombie
Z.x = Math.random() * stage.stageWidth;
Z.y = Math.random() * stage.stageHeight;
Z.rotation = Math.random() * 360;
zombies.push(Z);
}
for (var count = 0; count<zombies.length; count ++)
{
zombies[count].loop();
}
}
}
}
Zombie Head
package
{
import flash.display.MovieClip;
import flash.events.Event;
public class ZombieHead extends MovieClip
{
var headSpeed:Number = -30;
var friction:Number = 0.9;
public function ZombieHead()
{
// constructor code
addEventListener(Event.ENTER_FRAME,Update);
}
function Update(event:Event)
{
x = x+Math.cos(rotation/180*Math.PI)*headSpeed;
y = y+Math.sin(rotation/180*Math.PI)*headSpeed;
headSpeed *= friction;
if (headSpeed > -0.00001)
{
removeEventListener(Event.ENTER_FRAME,Update);
parent.removeChild(this);
}
else if (x<0 || x > 550 || y < 0 || y > 400)
{
removeEventListener(Event.ENTER_FRAME,Update);
parent.removeChild(this);
}
}
}
}
Related
I have a problem: I have tested the single pong game, and then I found an error when the game live reached 0 and moved to a Game Over screen for a single pong game.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/loop()
Can you check this code?
Here is my code for the single pong game:
import flash.events.MouseEvent;
import flash.utils.Dictionary;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.display.MovieClip;
import flash.text.TextField;
import flash.net.SharedObject;
import flash.events.Event;
stop();
var sound: Sound
var sound_channel: SoundChannel;
var gameMiss: Miss = new Miss();
var gameBounce: BounceWall = new BounceWall();
var gameHit: Hit = new Hit();
var ballSpeedX: int = -3;
var ballSpeedY: int = -2;
var gameScore = 0;
var gameLives = 3;
var plzStop: Boolean = false;
helpContent.visible = false;
reset_btn.addEventListener(MouseEvent.CLICK, scoreReset);
function scoreReset(event: MouseEvent): void {
gameScore = 0;
gameLives = 3;
updateTextFields();
}
pause_btn.addEventListener(MouseEvent.CLICK, goPause);
function goPause(event: MouseEvent): void {
if (plzStop == true) {
plzStop = false;
} else {
plzStop = true;
}
}
home_btn.addEventListener(MouseEvent.CLICK, goHome);
function goHome(event: MouseEvent): void {
plzStop = true;
gotoAndStop("startScreen");
}
help_btn2.addEventListener(MouseEvent.CLICK, goHelp2);
function goHelp2(event: MouseEvent): void {
if (plzStop == true) {
helpContent.visible = false;
plzStop = false;
} else {
helpContent.visible = true;
plzStop = true;
}
}
function init(): void {
score_txt.text = gameScore;
lives_txt.text = gameLives;
stage.addEventListener(Event.ENTER_FRAME, loop);
}
function updateTextFields(): void {
score_txt.text = gameScore;
lives_txt.text = gameLives;
}
function calculateBallAngle(paddleY: Number, ballY: Number): Number {
var ySpeed: Number = 5 * ((ballY - paddleY) / 25);
return ySpeed;
}
function loop(e: Event): void {
if (plzStop == false) {
characterPaddle.y = mouseY;
blowfishPong.x += ballSpeedX;
blowfishPong.y += ballSpeedY;
if (blowfishPong.x <= blowfishPong.width / 2) {
blowfishPong.x = blowfishPong.width / 2;
ballSpeedX *= -1;
gameBounce.play();
}
else if (blowfishPong.x >= stage.stageWidth - blowfishPong.width / 2) {
blowfishPong.x = stage.stageWidth - blowfishPong.width / 2;
ballSpeedX *= -1;
gameMiss.play();
gameLives--;
lives_txt.text = gameLives;
if(gameLives == 0){
stage.removeEventListener(Event.ENTER_FRAME,loop);
gotoAndStop("gameover_Single");
SoundMixer.stopAll();
}
}
if (blowfishPong.y <= blowfishPong.height / 2) {
blowfishPong.y = blowfishPong.height / 2;
ballSpeedY *= -1;
gameBounce.play();
}
else if (blowfishPong.y >= stage.stageHeight - blowfishPong.height / 2) {
blowfishPong.y = stage.stageHeight - blowfishPong.height / 2;
ballSpeedY *= -1;
gameBounce.play();
}
if (characterPaddle.y - characterPaddle.height / 3 < 0) {
characterPaddle.y = characterPaddle.height / 3;
}
else if (characterPaddle.y + characterPaddle.height / 3 > stage.stageHeight) {
characterPaddle.y = stage.stageHeight - characterPaddle.height / 3;
}
if (characterPaddle.hitTestObject(blowfishPong) == true) {
if (ballSpeedX > 0) {
ballSpeedX *= -1;
ballSpeedY = calculateBallAngle(characterPaddle.y, blowfishPong.y);
gameScore++;
updateTextFields();
gameHit.play();
}
}
}
}
init();
Then, for the PlayerSkin function, there are some errors to fix:
character.addEventListener(Event.ENTER_FRAME, playerSkin);
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/goHome3()[BlowfishPong_fla.MainTimeline::frame28:55]
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at BlowfishPong_fla::MainTimeline/playerSkin()[BlowfishPong_fla.MainTimeline::frame28:268]
When I'm going to draw the next stage at frame 2 my enemies did nothing.
When the stage1 finished I remove the listeners and at stage 2 I add it back.
So, How can I use the array that I have at frame 1 to another frame?
stop();
var enemy1Array:Array = new Array();
for (var e1:int = numChildren - 1; e1 >= 0; e1--)
{
var childe1:DisplayObject = getChildAt(e1);
if (childe1.name.indexOf("enemy")>-1)
{
enemy1Array.push(MovieClip(childe1));
MovieClip(childe1).hitPoints=enemykoufoueshitpoints;
}
}
stage.addEventListener(Event.ENTER_FRAME,loop);
function loop(event:Event):void
{
for(var e:int=enemy1Array.length-1;e>=0;e--)
{
if (playerrun == true)
{
enemy1Array[e].x -=speedall;
}
if (playerrunback == true)
{
enemy1Array[e].x +=speedall;
}
if (enemy1Array[e].hitTestPoint(player.x+5,player.y-40))
{
trace("i heart the player");
zoihit++;
enemy1Array[e].x +=70;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestPoint(player.x-5,player.y-40))
{
trace("i heart the player");
zoihit++;
enemy1Array[e].x -=70;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestObject(knife)&&attackright == true)
{
attackright = false;
enemy1Array[e].hitPoints -= knifedamage;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitTestObject(knife)&&attackleft == true)
{
attackleft = false;
enemy1Array[e].hitPoints -= knifedamage;
enemy1Array[e].gotoAndPlay(1);
}
if (enemy1Array[e].hitPoints <= 0)
{
var thkiamantoui:thkiamanti= new thkiamanti;
diamondArray.push(thkiamantoui);
this.addChild(thkiamantoui);
thkiamantoui.gotoAndStop(2);
thkiamantoui.x = enemy1Array[e].x;
thkiamantoui.y = enemy1Array[e].y-5.05;
enemy1Array[e].parent.removeChild(enemy1Array[e]);
enemy1Array.splice(e,1);
}
if(enemy1Array[e].hitTestObject(dioswall))
{
enemy1Array[e].parent.removeChild(enemy1Array[e]);
enemy1Array.splice(e,1);
}
}
if(stage2.hitTestObject(playerhit))
{
stage.removeEventListener(Event.ENTER_FRAME,loop);
gotoAndPlay(2);
}
}
I am trying to make a game involving a dragon burning little knights, fairly simple but I am new to AS3 and can't seem to solve this problem, sometimes when I kill a knight it returns an output error saying:
TypeError: Error #1010: A term is undefined and has no properties.
at drogtor/onTick()
I can't find any relevant solutions elsewhere on this website so I am submitting a question myself.
Here is (what I think to be) the relevant code.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import global;
import flash.display.DisplayObject;
public class drogtor extends MovieClip
{
private var updown:Boolean=true;
private var fArray:Array;
private var eArray:Array;
var wpressed:Boolean = false;
var apressed:Boolean = false;
var dpressed:Boolean = false;
var spressed:Boolean = false;
var kspawn:Number = 5;
public function drogtor()
{
addEventListener(Event.ENTER_FRAME, onTick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fliy);
stage.addEventListener(KeyboardEvent.KEY_UP, fliy);
fArray = new Array();
eArray = new Array();
}
public function onTick(e:Event):void
{
var fcount:int=0;
var sdargon:int=10;
var rdargon:int=6;
var Angle:Number = (2 * Math.PI * (dargon.rotation/360));
var dy:Number = sdargon * Math.cos(Angle);
var dx:Number = sdargon * Math.sin(Angle);
HitBox.x = dargon.x;
HitBox.y = dargon.y;
HitBox.rotation = dargon.rotation;
//Flame Spewer
if (global.count==9)
{
var fAngle:Number = (2 * Math.PI * (dargon.rotation/360));
var fdy:Number = 10 * Math.cos(fAngle);
var fdx:Number = 10 * Math.sin(fAngle);
var ftemp:Flame=new Flame;
ftemp.x=dargon.x + (10 * fdx);
ftemp.y=dargon.y - (10 * fdy);
ftemp.rotation=dargon.rotation + Math.random() * (15-(-15)) + (-15);
fArray.push(ftemp);
addChildAt(ftemp, 0);
}
var kgen:int = Math.floor((Math.random() * 3)+1);
var stageside:int = Math.floor((Math.random() * 4)+1)
if (kgen == 1)
{
if (Math.floor((Math.random() * 100) + 1) <= kspawn)
{
var ktemp:keniget = new keniget;
if (stageside == 1)
{
ktemp.x = Math.random() * stage.stageWidth;
ktemp.y = 0
}
else if (stageside == 2)
{
ktemp.x = Math.random() * stage.stageWidth;
ktemp.y = stage.stageHeight;
}
else if (stageside == 3)
{
ktemp.x = 0;
ktemp.y = Math.random() * stage.stageHeight;
}
else if (stageside == 4)
{
ktemp.x = stage.stageWidth;
ktemp.y = Math.random() * stage.stageHeight;
}
eArray.push(ktemp);
addChildAt(ktemp, 0);
}
}
for (var iKl:int = eArray.length-1; iKl >= 0; iKl --)
{
var krotation:Number = (Math.atan2(eArray[iKl].y-dargon.y, eArray[iKl].x-dargon.x) * 180/Math.PI) - 90;
eArray[iKl].rotation = krotation
var kangle:Number = (2 * Math.PI * (eArray[iKl].rotation/360));
var edx:Number = 3 * Math.sin(kangle);
var edy:Number = 3 * Math.cos(kangle);
eArray[iKl].x += edx;
eArray[iKl].y -= edy;
if (eArray[iKl].hitTestObject(HitBox))
{
removeChild(eArray[iKl]);
eArray.splice(iKl, 1);
}
for (var iF:int=fArray.length-1; iF>=0; iF--)
{
if (eArray[iKl].hitTestObject(fArray[iF]))
{
removeChild(eArray[iKl]);
eArray.splice(iKl, 1);
kspawn += 0.5
}
}
}
for(var iFl:int=fArray.length-1; iFl>=0; iFl--)
{
if(fArray[iFl].currentFrame==fArray[iFl].totalFrames)
{
removeChild(fArray[iFl]);
fArray[iFl]=null;
fArray.splice(iFl, 1);
}
}
if(updown)
{
dargon.rotationX-=1;
if(dargon.rotationX == -10)
{
updown = false;
}
}
if(!updown)
{
dargon.rotationX+=1;
if(dargon.rotationX == 10)
{
updown = true;
}
}
//Movement
if(wpressed)
{
dargon.x += dx;
dargon.y -= dy;
}
if(apressed)
{
dargon.rotation -= rdargon;
}
if(dpressed)
{
dargon.rotation += rdargon;
}
if(spressed)
{
dargon.x -= ((1/4) * dx);
dargon.y += ((1/4) * dy)
}
}
public function fliy(ke:KeyboardEvent):void
{
if(ke.type == "keyDown")
{
//key=A
if(ke.keyCode==87)
{
wpressed=true;
}
//Key=A
if(ke.keyCode==65)
{
apressed=true;
}
//Key=D
if(ke.keyCode==68)
{
dpressed=true;
}
if(ke.keyCode==83)
{
spressed=true;
}
}
if(ke.type == "keyUp")
{
//key=A
if(ke.keyCode==87)
{
wpressed=false;
}
//Key=A
if(ke.keyCode==65)
{
apressed=false;
}
//Key=D
if(ke.keyCode==68)
{
dpressed=false;
}
if(ke.keyCode==83)
{
spressed=false;
}
}
}
}
}
The problem is, that even though you removed the knight from the display list, the code is still running in the onTick function, and you are trying to access something that is not existing, I suggest you to check whether your knight's reference is null or not, if not, then run the code, else skip it.
EDIT:
Whenever you null an instance (myInstance = null), you can no longer access to it's properties, functions etc.. and if you try, then you get that error above. To avoid it, check whether your object is null or not.
if(myInstance != null) {
//do your stuff with your instance
}
I am trying to create a cone of vision for my enemy class. Right now it checks to see if the player is within a radius before moving towards them and will move around randomly if they are not. I want to give it vision so the enemy does not always rotate towards the player.
Enemy Class
package
{
import flash.automation.ActionGenerator;
import flash.events.Event;
import flash.geom.Point;
public class Enemy extends GameObject
{
var isDead:Boolean = false;
var speed:Number = 3;
var originalValue:Number = 50;
var changeDirection:Number = originalValue;
var checkDirection:Number = 49;
var numberGen:Number;
//var startTime:int = getTimer();
//var $timer = getTimer();
//var myTimer:int = getTimer();
//var moveTimer:int = timer - $timer;
public function Enemy()
{
super();
target = target_mc;
}
override public function update():void
{
movement();
}
private function movement():void
{
if (changeDirection >= 0)
{
if (changeDirection > checkDirection)
{
numberGen = (Math.random() * 360) / 180 * Math.PI;
}
var velocity:Point = new Point();
var $list:Vector.<GameObject > = getType(Player);
var $currentDistance:Number = Number.MAX_VALUE;
for (var i:int = 0; i < $list.length; i++)
{
var currentPlayer:GameObject = $list[i];
if (MathUtil.isWithinRange(currentPlayer.width,currentPlayer.x,currentPlayer.y,width,x,y))
{
var $delta:Point = new Point ;
$delta.x = currentPlayer.x - x;
$delta.y = currentPlayer.y - y;
if ($currentDistance > $delta.length)
{
$currentDistance = $delta.length;
velocity = $delta;
velocity.normalize(speed);
}
}
}
if(velocity.length == 0)
{
velocity = Point.polar(2, numberGen);
}
changeDirection--;
if (changeDirection == 0)
{
changeDirection = originalValue;
}
}
velocity.x = Math.floor(velocity.x * 10) * .1;
velocity.y = Math.floor(velocity.y * 10) * .1;
moveBy([Wall, Door], velocity.x, velocity.y, collides_mc);
var $collides:GameObject = collision([Player]);
if ($collides != null)
{
destroy();
}
if ($collides == null)
{
$collides = collision([Player],this);
if ($collides != null)
{
$collides.destroy();
}
}
rotation = (Math.atan2(velocity.y, velocity.x) * 180 / Math.PI);
collides_mc.rotation = -rotation;
trace($collides);
}
override public function onCollideX($collision:GameObject):void
{
}
override public function onCollideY($collision:GameObject):void
{
}
}
}
GameObject Class, which contains all the functions and Enemy extends it
package
{
import flash.display.DisplayObject;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.sensors.Accelerometer;
import flashx.textLayout.elements.ListElement;
public class GameObject extends MovieClip
{
static public var list:Vector.<GameObject> = new Vector.<GameObject>;
protected var hitBox:Sprite;
public var target:MovieClip;
public function GameObject()
{
target=this;
list.push(this);
}
public function update():void
{
}
public function collision(typesColliding:Array, $target:DisplayObject = null):GameObject
{
if($target == null)
$target = target;
for (var i:int = list.length - 1; i >= 0; i--)
{
var item:GameObject = list[i], found:Boolean = false;
for (var f:int = typesColliding.length - 1; f >= 0; f--)
{
if (item is typesColliding[f])
{
found = true;
break;
}
}
if (found && $target.hitTestObject(item.target) && this != item)
{
return item;
}
}
return null;
}
public function moveBy(typesColliding:Array, $x:Number = 0, $y:Number = 0, $target:DisplayObject = null):void
{
var $collision:GameObject;
x += $x;
if (($collision = collision(typesColliding, $target)) != null)
{
x -= $x;
onCollideX($collision);
}
y += $y;
if (($collision = collision(typesColliding, $target)) != null)
{
y -= $y;
onCollideY($collision);
}
}
public function onCollideX($collision:GameObject):void
{
}
public function onCollideY($collision:GameObject):void
{
}
public function getType($class:Class):Vector.<GameObject>
{
var $list:Vector.<GameObject> = new Vector.<GameObject>;
for (var i:int = 0; i < list.length; i++)
{
if (list[i] is $class)
{
$list.push(list[i]);
}
}
return $list;
}
public function destroy():void
{
var indexOf:int = list.indexOf(this);
if (indexOf > -1)
list.splice(indexOf, 1);
if (parent)
parent.removeChild(this);
trace("removing item: "+this+" list: " + list.length + ": " + list);
}
}
}
Any help would be appreciated, thank you.
Changing radius of vision to the cone of vision simply requires computing (after checking the distance) the angle between the direction enemy is facing and the player. If this angle is smaller then some T, then it is in the cone of vision of angle 2T (due to symmetry).
This obviously ignores any walls/obstacles that should limit vision, but it should give you the general idea.
Just as the name says, I want that for each certain name in an array a value is added to a int.
For example: if there are 3 strings of the same name in the array, then 3 times 50 will be added to the value.
This is my script I have now:
var lootList = new Array();
var interaction : Texture;
var interact = false;
var position : Rect;
var ching : AudioClip;
var lootPrice = 0;
function Update()
{
print(lootList);
if ("chalice" in lootList){
lootPrice += 50;
}
}
function Start()
{
position = Rect( ( Screen.width - interaction.width ) /2, ( Screen.height - interaction.height ) /2, interaction.width, interaction.height );
}
function OnTriggerStay(col : Collider)
{
if(col.gameObject.tag == "loot")
{
interact = true;
if(Input.GetKeyDown("e"))
{
if(col.gameObject.name == "chalice")
{
Destroy(col.gameObject);
print("chaliceObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("chalice");
}
if(col.gameObject.name == "moneyPouch")
{
Destroy(col.gameObject);
print("moneyPouchObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("moneyPouch");
}
if(col.gameObject.name == "ring")
{
Destroy(col.gameObject);
print("ringObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("ring");
}
if(col.gameObject.name == "goldCoins")
{
Destroy(col.gameObject);
print("coldCoinsObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("goldCoins");
}
if(col.gameObject.name == "plate")
{
Destroy(col.gameObject);
print("plateObtained");
audio.clip = ching;
audio.pitch = Random.Range(0.8,1.2);
audio.Play();
interact = false;
lootList.Add("plate");
}
}
}
}
function OnTriggerExit(col : Collider)
{
if(col.gameObject.tag == "pouch")
{
interact = false;
}
}
function OnGUI()
{
if(interact == true)
{
GUI.DrawTexture(position, interaction);
GUI.color.a = 1;
}
}
It's for a game I'm making where you can steal items for extra score points.
I've tried using the for(i = 0; i < variable.Length; i++) but that didn't seem to work.
The only thing I can think of now is using booleans to add it once. But that isn't memory friendly.
Help is appreciated and thanks in advance!
You could use the standard .forEach(callback) method:
lootList.forEach(function(value, index, array)
{
if (value === "chalice") { lootPrice += 50; }
});
If you don't have that method, you could implement it like this:
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (callback) {
for(var i = 0; i < this.length; i++) { callback(this[i], i, this); }
}
}