gametechmods
Off-Topic => Off-Topic Discussion => Topic started by: Doomkiller on September 07, 2011, 01:56:38 AM
-
Yes, I know. Moved from python to ActionScript now :P
Anyway, if anyone is skilled in the way of actionscript, can you please tell me how to fix this problem?
Here is the code:
stop();
// variables used //
var score:Number = 0;
var bangSound:Sound = new fire();
var distance:Number = 0;
var adjDistance:Number = 0;
var radians:Number = 0;
var dx:Number = 0;
var dy:Number = 0;
var gunLength:uint = 90;
var bullets:Array = new Array();
var bulletSpeed:uint = 20;
var baddies: Array = new Array();
var specialbaddies: Array = new Array();
var timer:Timer = new Timer(1000);
timer.start();
var side:Number = 0;
var life:Number = 3
var target:MovieClip;
// event listeners//
stage.addEventListener(MouseEvent.MOUSE_MOVE, aimGun);
stage.addEventListener(MouseEvent.MOUSE_MOVE, targetMove);
stage.addEventListener(MouseEvent.MOUSE_DOWN, fireGun);
stage.addEventListener(Event.ENTER_FRAME, moveObjects);
timer.addEventListener(TimerEvent.TIMER, addBaddie);
timer.addEventListener(TimerEvent.TIMER, addspecialBaddie);
//Outputs life and score//
Pscore.text = String(score);
count.text = String(life);
//aims gun to the mouse//
function aimGun(evt:Event):void {
gun.rotation = getAngle(gun.x, gun.y, mouseX, mouseY);
distance = getDistance(gun.x, gun.y, mouseX, mouseY);
adjDistance = distance / 12 - 7;
range.text = "Range: " + String(adjDistance.toFixed(2)) + "m";
range.x = mouseX + 20;
range.y = mouseY - 10;
}
//gets the angle//
function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number {
radians = Math.atan2(y2 - y1, x2 - x1);
return rad2deg(radians);
}
//gets the distance//
function getDistance(x1:Number, y1:Number, x2:Number, y2:Number):Number {
dx = x2 - x1;
dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
//turns radians to degrees//
function rad2deg(rad:Number):Number {
return rad * (180 / Math.PI);
}
//turns the cursor to the movie clip//
initialiseCursor();
function initialiseCursor():void {
Mouse.hide();
target = new Target();
target.x = mouseX;
target.y = mouseY;
target.mouseEnabled = false;
addChild(target);
}
//moves the movie clip to follow the mouse//
function targetMove(evt:MouseEvent):void {
target.x = this.mouseX;
target.y = this.mouseY;
}
//fires the bullet on mouse click in direction of mouse//
function fireGun(evt:MouseEvent) {
bangSound.play();
var bullet:Bullet = new Bullet();
bullet.rotation = gun.rotation;
bullet.x = gun.x+Math.cos(deg2rad(gun.rotation))*gunLength;
bullet.y = gun.y+Math.sin(deg2rad(gun.rotation))*gunLength;
addChild(bullet);
bullets.push(bullet);
}
//turns degrees to radians//
function deg2rad(deg:Number):Number{
return deg*(Math.PI/180);
}
//calls the function to move the baddies and bullets//
function moveObjects(evt:Event):void {
moveBullets();
moveBaddies();
moveSpecialBaddies();
}
//moves the bullets//
function moveBullets():void{
for (var i:int = 0; i < bullets.length; i++) {
dx = Math.cos(deg2rad(bullets[i].rotation)) * bulletSpeed;
dy = Math.sin(deg2rad(bullets[i].rotation))* bulletSpeed;
bullets[i].x += dx;
bullets[i].y += dy;
if (bullets[i].x < -bullets[i].width || bullets[i].x > stage.stageWidth + bullets[i].width || bullets[i].y < -bullets[i].width || bullets[i].y > stage.stageHeight + bullets[i].width) {
removeChild(bullets[i]);
bullets.splice(i, 1);
}
}
}
//adds the baddies randomly//
function addBaddie(evt:TimerEvent): void {
var baddie:Baddie = new Baddie();
side = Math.ceil(Math.random() * 4);
if (side == 1) {
baddie.x = Math.random()*stage.stageWidth;
baddie.y = - baddie.height;
} else if (side == 2) {
baddie.x = stage.stageWidth + baddie.width;
baddie.y = Math.random() * stage.stageHeight;
} else if (side == 3) {
baddie.x = Math.random() * stage.stageWidth;
baddie.y = stage.stageHeight + baddie.height;
} else if (side == 4) {
baddie.x = - baddie.width
baddie.y = Math.random() * stage.stageHeight;
}
baddie.angle = getAngle(baddie.x, baddie.y, gun.x, gun.y)
baddie.speed = Math.ceil(Math.random()* 15);
addChild(baddie);
baddies.push(baddie);
}
//adds the special baddie//
function addspecialBaddie(evt:TimerEvent): void {
side = Math.ceil(Math.random() * 40);
if (side < 5) {
var specialbaddie:SpecialBaddie = new SpecialBaddie();
if (side == 1) {
specialbaddie.x = Math.random()*stage.stageWidth;
specialbaddie.y = - specialbaddie.height;
} else if (side == 2) {
specialbaddie.x = stage.stageWidth + specialbaddie.width;
specialbaddie.y = Math.random() * stage.stageHeight;
} else if (side == 3) {
specialbaddie.x = Math.random() * stage.stageWidth;
specialbaddie.y = stage.stageHeight + specialbaddie.height;
} else if (side == 4) {
specialbaddie.x = - specialbaddie.width
specialbaddie.y = Math.random() * stage.stageHeight;
}
specialbaddie.angle = getAngle(specialbaddie.x, specialbaddie.y, gun.x, gun.y)
specialbaddie.speed = Math.ceil(Math.random()* 20);
addChild(specialbaddie);
specialbaddies.push(specialbaddie)
}
}
//moves the baddies towards the turrent and c if they hit the turrent//
function moveBaddies():void {
for (var i:int = 0; i < baddies.length; i++) {
dx = Math.cos(deg2rad(baddies[i].angle)) * baddies[i].speed;
dy = Math.sin(deg2rad(baddies[i].angle)) * baddies[i].speed;
baddies[i].x +=dx;
baddies[i].y +=dy;
if (baddies[i].hitTestPoint(gun.x, gun.y, true)) {
removeChild(baddies[i]);
baddies.splice(i, 1);
loseLife();
}else{
checkForHit(baddies[i]);
}
}
}
//moves the special baddies//
function moveSpecialBaddies():void {
for (var i:int = 0; i < specialbaddies.length; i++) {
dx = Math.cos(deg2rad(specialbaddies[i].angle)) * specialbaddies[i].speed;
dy = Math.sin(deg2rad(specialbaddies[i].angle)) * specialbaddies[i].speed;
specialbaddies[i].x +=dx;
specialbaddies[i].y +=dy;
if (specialbaddies[i].hitTestPoint(gun.x, gun.y, true)) {
removeChild(specialbaddies[i]);
specialbaddies.splice(i, 1);
loseLife();
}else{
checkFor2Hit(specialbaddies[i]);
}
}
}
//c if the bullets hit the baddie//
function checkForHit(baddie:Baddie):void {
for (var i:int = 0; i < bullets.length; i++) {
if (baddie.hitTestPoint(bullets[i].x, bullets[i].y, true)) {
removeChild(baddie);
score = score + 20;
Pscore.text = String(score);
baddies.splice(baddies.indexOf(baddie), 1);
}
}
}
//c if bullet hit special baddie//
function checkFor2Hit(specialbaddie:Baddie):void {
for (var i:int = 0; i < bullets.length; i++) {
if (specialbaddie.hitTestPoint(bullets[i].x, bullets[i].y, true)) {
removeChild(specialbaddie);
score = score + 200;
Pscore.text = String(score);
specialbaddies.splice(specialbaddies.indexOf(specialbaddie), 1);
}
}
}
//c if all lifes a lost, to which it gets rid of all baddies and enters next frame//
function loseLife():void {
life -= 1;
count.text = String(life);
if (life < 0){
gotoAndStop(3);
if(baddies.length > 0){
for (var i:int = 0; i < baddies.length; i++)
{removeChild(baddies[i]);}
}
if(bullets.length > 0) {
for (var i = 0; i < bullets.length; i++)
{removeChild(bullets[i]);}
}
if (specialbaddies.length > 0){
for (var i:int = 0; i < specialbaddies.length; i++)
{removeChild(specialbaddies[i]);}
}
timer.stop();
removeChild(target);
Mouse.show();
stage.removeEventListener(MouseEvent.MOUSE_MOVE, aimGun);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, targetMove);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, fireGun);
stage.removeEventListener(Event.ENTER_FRAME, moveObjects);
}
}
And the error I keep getting:
TypeError: Error #1034: Type Coercion failed: cannot convert SpecialBaddie@11c436a1 to Baddie.
at ShootingGame_fla::MainTimeline/moveSpecialBaddies()
at ShootingGame_fla::MainTimeline/moveObjects()
Now, the game does work, however, whenever the "specialbaddie" enters the stage, that error happens. The 'specialbaddie' still moves, but the collision detection does not work (ie, when the bullet hits the specialbaddie) and even though I made a seperate function for it, it does not work :(
Any help would be appreciated
(for those interested, I can send you the flash file to have a look of the game)
-
I've never programmed in AS, but I guess in your function checkFor2Hit, the argument should be of SpecialBaddie type, not Baddie (unless you find a way to convert a SpecialBaddie to a Baddie object in AS - in other languages this is called polymorphism).
-
I've never programmed in AS, but I guess in your function checkFor2Hit, the argument should be of SpecialBaddie type, not Baddie (unless you find a way to convert a SpecialBaddie to a Baddie object in AS - in other languages this is called polymorphism).
Thank you so much, can't believe I didn't think of that before :D
The game works fine now, so if anyone wants to try it, inbox me and I'll send it to you :)
-
Also, you should keep using Python, it's a much wider used language than AS. And yes, you can write games with it. Check out PyGame (very easy game library) or pyglet (very advanced multimedia and OpenGL framework).
-
Also, you should keep using Python, it's a much wider used language than AS. And yes, you can write games with it. Check out PyGame (very easy game library) or pyglet (very advanced multimedia and OpenGL framework).
It's only because im doing AS in class, and Python was for the programming challenge that I was doing