Checking shot collisions and removing shot and target in AS3 - arrays

Last year was my first successful year in teaching my students to create catch and avoid games in Flash with AS3. This year is getting better. Each year I come here for help at least once.
I'm trying to add shooting into the possibilities for their second game project. I can make the shot happen from the ship, gun, whatever, and make it move, and get rid of it when it is off screen, but have not figured out a clean way to have both the shot and the target go away (removeChild and array.splice) upon collision.
The code I have sort of works, but I keep getting the error,
TypeError: Error #1010: A term is undefined and has no properties. at
DropShootV02_fla::MainTimeline/checkShots()
.
Normally I know that this is because of a mismatch between objects and index numbers, but this is related to the call to a second array in removing boxes and bullets.
For simplicity I'll just include the shooting code. Similar organization creates and drops the boxes.
Any help is appreciated. BTW we are not using external script in an AS file.
var shotSpeed = 18;
var shots:Array = new Array();
import flash.events.MouseEvent;
import flash.events.Event;
stage.addEventListener(MouseEvent.CLICK, fireLaser);
function fireLaser(e:MouseEvent):void
{
if (gameOn==true)
{
var shot:Shot=new Shot();
addChild(shot);
shots.push(shot);
shot.gotoAndStop(1);
shot.x = user.x;
shot.y = user.y;
trace(shots.length);
}
}
addEventListener(Event.ENTER_FRAME, moveShot);
function moveShot(e:Event):void
{
for (var i:int=shots.length-1; i>=0; i--)
{
shots[i].y -= shotSpeed;
if (shots[i].y < -25)
{
removeChild(shots[i]);
shots.splice(i,1);
}
}
}
addEventListener(Event.ENTER_FRAME, checkShots);
function checkShots(e:Event):void
{
for (var i:int=shots.length-1; i>=0; i--)
{
for (var k:int=boxes.length-1; k>=0; k--)
{
if (shots[i].hitTestObject(boxes[k]))
{
if (boxes[i].type == "good")
{
score--;
scoreDisplay.text = "Score:" + score;
}
if (boxes[i].type == "bad")
{
score++;
}
removeChild(boxes[k]);
boxes.splice(k,1);
//if I leave this part out I get no errors,
//but but the bullet goes on to strike again
removeChild(shots[i]);
shots.splice(i,1);
}
}
}
}
Thanks kaarto:
I had tried that previously and kept getting the same error. I used that elsewhere in this game code. Turns out I needed to moderate how often the player was shooting. I changed from shooting with the mouse to using space instead and now the problem is gone. Break is definitely a good one here.

Merge move shot and checkShots in one ENTER_FRAME handler.

Related

Using arrays and hitTest - AS3

From what my friend has told me, this should be working but it is not.
var P2hb:Array = new Array(P2char1, P2char2, P2char3);
var P2life:Number = 0;
addEventListener(Event.ENTER_FRAME, framecheck)
function framecheck(event:Event):void
{
if (P2hb.hitTestObject(P1attack)) { P2life-=2; }
}
This is a generic code but it is the same as what I have. Basically,
all elements in the P2hb are movieclips on the stage.
I want to say that, if P1attack hits any of the objects in the array, then P2life will drop by 2, without having to type hitTestObject() for each individual object.
I can't seem to get it to work. Can anyone tell me what I am doing wrong?
Thank you in advance.
Simply, loop through each individual "movieClip" in the array (using a for loop, for example) and check for the collision against your other object:
function framecheck(event:Event):void
{
for each (var enemy in P2hb) {
if (enemy.hitTestObject(P1attack)) {
P2life-=2;
trace("hit occurred! P2life: "+P2life);
}
}
}

can't get delete function to work properly AS3

I have a Volcano class. It's purpose is to randomly spray lavaMass.
In the main class I have made it so that when my character hits the lava, everything is removed, and the deleteLava() function is called.
My problem is that with the deleteLava() function, my Volcano class stops making lava after a few 'deaths'.
Now i gets a little complicated to explain.
I found out that inside the deleteLava() function I should also remove the lavaMass from the array, for it to keep making lava no matter how many 'deaths'.
I used 'lavaSpray.shift();', and tried 'splice'..
And this worked! Except that it doesn't delete all the lavaMass, so everytime the character dies a few of the lavaMass objects keeps hanging around in the level, not moving. So after many 'deaths' there is a lot of lavaMass objects just standing still, not moving and not being deleted.
Here's the volcano class:
package {
import flash.display.MovieClip;
import flash.events.Event;
public class Volcano extends MovieClip {
var lavaSpray:Array = [];
var lavaNum:Number;
var world:World;
public function LavaThing() {
addEventListener(Event.ENTER_FRAME, makeLava)
}
public function deleteLava()
{
this.parent.removeChild(this)
for (var m = 0; m < lavaSpray.length; m++) {
if (lavaSpray[m].parent) {
lavaSpray[m].parent.removeChild(lavaSpray[m]);
}
}
}
function makeLava (e:Event)
{
lavaNum = Math.random()*70
if (lavaSpray.length<20)
{
if (lavaNum > 68.15)
{
var lavaMass = new LavaMass();
lavaSpray.push(lavaMass);
addChild(lavaMass);
lavaMass.scaleY = 0.5;
lavaMass.scaleX = 0.5;
lavaMass.x += 90;
lavaMass.y += Math.random()*30
lavaMass.y -= Math.random()*30
}
}
for each (lavaMass in lavaSpray)
{
lavaMass.x += 4;
if (lavaMass.parent && lavaMass.x > 800)
{
lavaMass.parent.removeChild(lavaMass);
lavaSpray.shift();
}
}
}
}
}
I hope someone knows what I'm talking about. Thanks in advance.
I put up a video with my problem:
https://www.youtube.com/watch?v=rYxYHB7rcgY&feature=youtu.be
I suspect the issues is:
you don't remove the ENTER_FRAME Event Listener when you delete the lava.
removing the child doesn't mean you deleted it's listener.
so you may remove all the lava , but still the ENTER_FRAME creates more !
you have to remove it your self.
so you have to add
removeEventListener(Event.ENTER_FRAME, makeLava)
as the first line at your function deleteLava()
....
or you may add
addEventListener(Event.REMOVED_FROM_STAGE, onRemoved)
and create onRemoved(e:Event){} function
then inside this function remove any listener you Added , including the ENTER_FRAME & the REMOVED_FROM_STAGE
try to read more about these subjects
Game Loop
entity system framework
Pool Object pattern
I may but links , but I prefer you Google them , and read from different resources - whatever the language they are explained with -
good luck
just for clarrifying your question.. You added the lavamass inside the volacano class? and you need to remove lavamass or volcano ? Dp you get any error during compilation? Also i encountered some thing like this when developing a shooting game, the removed bullets shows up after removing the bullerts. What i did was , i just added the bullets into a empty movieclip and removed the entire clip.

hitTestPoint or hitTestObject?

I'm needing to make my character land on a ledge and stay there, but it only keeps going straight through it. Would I create an array for all my different ledges and test whenever my character hits them? Any help would be appreciated.
Thanks.
Collision detection for floors and stuff is actually alot diffrent from hitTesting in the idea that needs to consistently see that the objects are touching. Try something like this!
//loop through all the platform objects to generate the level
var level:Array = new Array();
for (var i=0; i<numChildren; i++)
{
if (getChildAt(i) is platform)
{
level.push(getChildAt(i).getRect(this));
}
}
for (i=0; i<level.length; i++)
{
if (player.getRect(this).intersects(level[i]))
{
if (speedX > 0) ////moving right collision and stuffs
{
player.x = level[i].left-player.width/2;
}
if (speedX < 0) ////moving left collision and stuffs
{
player.x = level[i].right+player.width/2;
}
speedX = 0 //kills the speed
}
}
speedX is the speed at which the characters move horizontally, and "platform" is the name of the variable that you're using as the cliff. Also, "player" can be substituted by whatever you are calling your object that's going onto the ledge. That's how I did it in one of my computer classes anyways :) Hope that helps!

AS3: Creating and accessing movieclips from an array

I know there are quite similar questions here, but I haven't found the proper details. What would be helpful is definitely an explanation of the problems, and perhaps a base example, that anyone who searches later may be able to apply. (Not asking that you write it for me, I just find the examples helpful) I don't want to upset anyone and am kind of worried to post in a forum...
I am wondering alternatives to creating a screen based off tiles created from an array. I have been having an issue myself trying to access the movieclips that have been placed on screen, and trying to trace to find a way to reference them hasn't been working.
Anyway, take something basic like an array, and connecting it to movieclips, then how to access the movieclip itself once done. So I have been working on this, and used many different online resources, so I'm sure a lot of this is going to look familiar, just in a much messier way.
This takes the array to make the movieclips appear (Im sure at least one part in here is unnecessary, and I'm thinking I'm doing something wrong here that makes it not work out later) So this works, but feels pretty bulky.
Both are from the same main class file.
function makeWorld (anyMap, tileW, tileH) {
var worldWidth = anyMap[0].length;
var worldHeight = anyMap.length;
var MAP = this.addChild(new mapHolder());
function tiler(MAP, i, j, tileW, tileH, tile)
{
MAP.addChild(tile);
tile.x = (j * tileW);
tile.y = (i * tileH);
}
for (var i = 0; i < worldWidth; ++i) {
for (var j = 0; j < worldHeight; ++j) {
var curTile:int = anyMap[i][j];
if (curTile == 101) {
var tile1 = new tileGround();
tiler (MAP, i, j, tileW, tileH, tile1);
...
else {
var tile3 = new empty();
tiler (MAP, i, j, tileW, tileH, tile3);
}
}}}
Then there is attempting to reference it, where I'm having the issue. I don't know what to call this.MAP.tileGround by, and I have tried many things. I've read it's not such a good idea to reference by name when not very advanced so I wanted to avoid that sort of thing too.
addEventListener (Event.ENTER_FRAME, hits);
function hits (event:Event) {
var tileCatchG:MovieClip = this.MAP.tileGround;
if(tileCatchG.hitTestPoint(this.MAP.Char.x + leftBumpPoint.x, this.MAP.Char.y + leftBumpPoint.y, true)){
leftBumping = true;
} else {
leftBumping = false;
}
...
}
Thank you!
In looking over what you're doing a second time it would appear that you should have a reference to the 2-indexed array that represents the map.
You can create a regular (single indexed) Array at the top of the file like
public var tileArray:Array = [];
Then where you create them push them into the array
var tile1 = new tileGround();
tileArray.push(tile1);
then to reference them all you can just run a simple loop
for each(var tile:MovieClip in tileArray)
{
//Do stuff
if(tile instanceof tileGround)
{
//Do stuff specific to tileGround
}
}

Flash Resetting array values

I was working on a test project that I will later incorporate into a much larger work that is a simple quiz game. I made an array with my questions:
var questions1:Array=["nitrogen dioxide","sulfur hexafluoride",..."]
and in a second layer I made a button that cycles through the questions randomly.
import flash.events.MouseEvent;
var qno=0;var rnd1;
function change_question(){
rnd1=Math.ceil(Math.random()*questions1.length)-1;
q.text=questions1[rnd1];
if(questions1[rnd1]=="X"){change_question();}
questions1[rnd1]="X";
}
change_question();
next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
function ButtonAction1(eventObject:MouseEvent){
qno++;change_question();
}
This part works great, because I was following a tutorial. Text appears in a dynamic text box I created as it should. This tutorial taught to change the value of the array to X with every choice and to ignore choose a different question every time it encountered an X
After it cycles through all the questions I basically get an infinite loop in my output section of flash because it can't find any more non-X values. I was hoping someone had information on how to press a button an change the array back to its default settings so that a teacher (because that is who it is for) has a way of resetting the file when they have reached the end of the quiz.
Thanks everyone!
As per my understanding you wanted to randomize your questions and after showing all the entire questions you wanted to reset your questions.
As per your code you get a random question and updating the same array by pushing value 'X'. Rather than doing this what you need to do is to preserve the array only randomize its position. So that you can use the same value once you cover your all questions
I have added code here.
import flash.events.MouseEvent;
var qno=0;var rnd1;
var questions1:Array=["nitrogen dioxide","sulfur hexafluoride","carbon dioxide","carbon monooxide"];
var nAttmeptedCount = 0;
var shuffledLetters:Array;
function change_question()
{
if(qno == questions1.length)
{
qno = 0;
resetQuestion()
}
else
{
q.text = questions1[shuffledLetters[qno]];
qno++;
}
}
function resetQuestion()
{
shuffledLetters = new Array(questions1.length);
for(var i=0;i<shuffledLetters.length;i++)
{
shuffledLetters[i] = i;
}
shuffledLetters.sort(randomize);
}
function randomize ( a : *, b : * ) : int {
return ( Math.random() > .5 ) ? 1 : -1;
}
resetQuestion()
change_question();
next_b.addEventListener(MouseEvent.CLICK, ButtonAction1);
function ButtonAction1(eventObject:MouseEvent){
change_question();
}
In above solution after showing all questions I have reset the questions automatically. you can modify code as per your requirement. If you want to do the reset questions you can put you code qno = 0;resetQuestion() on button click.
hope above solution work for you.

Resources