ActionScript 3.0 :: Hit Detection On Array?
Mar 31, 2011
i have a snake game, that after time loads sprites in an array to make the snake grow. Just wondering how i would go about adding a hit so when the head of the snake hits the body something happens. Below is the full document class code.
<code>
package {
import flash.display.Sprite;[CODE].....
View 5 Replies
Similar Posts:
Oct 14, 2011
Hey guys, thanks in advance for any help, I'd be stuck all the time without these forums.
I have a custom class(Ball) and a .FLA. Ignore the custom class, it works fine, having a problem within my main FLA.
I'm getting repeated "Error #1009: Cannot access a property or method of a null object reference. at Environment_fla::MainTimeline/hitTest()".
I understand that this error means I'm asking it to reference something that's no longer there. I just can't figure out why I'm getting it, and how better to hitTest many objects in an array.
My game involves creating a bunch of balls that will fall into buckets. I need to keep track of how many touched the buckets, how many fell off the screen, and remove each accordingly.
Thanks again, very much appreciated.
Here is the code from my main FLA
[as]var ballTimer:Timer = new Timer(200);
var changeTimer:Timer = new Timer(50000);
ballTimer.addEventListener(TimerEvent.TIMER, throwBall, false, 0, true);
ballTimer.start();
changeTimer.addEventListener(TimerEvent.TIMER, changeColor);
changeTimer.start();
//Environment variables
var gravity:int = 1.5;
var friction:Number = .85;
var color:int = 2;
//score variables
var YellowScore:int = 0;
var BlackScore:int = 0;
var tBall:Ball = new Ball(new Point(mouseX, mouseY), new Point(Math.random() + Math.random()*5 + Math.random()*8), gravity, friction);
var ballArray:Array = new Array();
function throwBall(e:TimerEvent):void {
var tBall:Ball = new Ball(new Point(mouseX, mouseY), new Point(Math.random() + Math.random()*5 + Math.random()*8), gravity, friction);
tBall.addEventListener(Event.ENTER_FRAME, hitTest);
tBall.gotoAndStop(color);
addChild(tBall);
ballArray.push(tBall);
}
function changeColor(e:TimerEvent):void {
if (color == 1) {
color = 2;
return;
}
if (color == 2) {
color = 1;
}
}
function hitTest(e:Event) {
trace(ballArray.length);
for (var i = 0; i < ballArray.length - 1 ; i++) {
if ((ballArray[i].hitTestObject(YellowBucket1) && ballArray[i].currentFrame == 2)) {
YellowScore += 1; //trace(YellowScore); trace("Space");
ballArray[i].parent.removeChild(ballArray[i]);
ballArray.shift();
break;
}
else if ((ballArray[i].hitTestObject(YellowBucket1) && ballArray[i].currentFrame == 1)) {
YellowScore -= 1;
}
else if ((ballArray[i].hitTestObject(BlackBucket1) && ballArray[i].currentFrame == 2)) {
BlackScore -= 1;
}
else if ((ballArray[i].hitTestObject(BlackBucket1) && ballArray[i].currentFrame == 1)) {
BlackScore += 1; trace("BlackScore");
}
else if (ballArray[i].y > stage.stageHeight || ballArray[i].x > stage.stageWidth) {
trace("cleanUp");
ballArray[i].parent.removeChild(ballArray[i]);
ballArray.shift();
}
}
updateScore();
}
function updateScore() {
BlackBucket_txt.text = String(BlackScore);
YellowBucket_txt.text = String(YellowScore);
}
[/as]
View 3 Replies
Mar 28, 2012
I'm trying to have an enemy's shell hit my player. I have the logic in place that detects a player's bomb hit an enemy, I tried to edit it slightly and apply the same logic to the enemies shell.
Code:
// check for collisionspublic function checkHits(event:Event)
{
[code]......
View 2 Replies
Sep 18, 2011
I have an array of points that I will use to generate a closed polygonal fence on the outside of a game stage (2D). I wish to have collision detection between this fence and a bouncing ball-like object on the inside. Additionally, I would like to be able to arbitrarily add/remove/redraw the fence in realtime and have the collision detection still operate realistically. I have considered drawing a Sprite/Shape from the points and doing a HitTest at each frame to check whether to bounce or not. My question: is this the best/correct way to accomplish this goal? Consider something like JezzBall with diagonal lines of any angle a simulation of what I'm trying to do.
View 2 Replies
Jun 23, 2009
I am creating a game where I have two separate arrays of objects that I need hitTested with each other. I found an old tutorial in AS2 that I modified to come up with this code:
ActionScript Code:
for (var w:Number = 0;w < enemyLaserArray.length;w++) {
for (var R:Number = 0;R < barricade1Array.length-1;R++) {
if (enemyLaserArray[w].hitTestObject(barricade1Array[R])) {
[code]....
This code executes like I believe it should, but along with removing the two objects, and splicing them from their arrays, I get this error
Code:
TypeError: Error #1010: A term is undefined and has no properties.on the line hitTestObject line. I have had similar problems in other areas of my project, but this time no matter what I try using trace statements I cannot fathom quite what the problem is.
View 2 Replies
Nov 13, 2009
a little while ago I saw something on this website:[URL]...I decided I wanted to recreate something like it.
They're using javascript, I'm using ActionScript, anyway.I'm trying to create collision detection for an array of movieclips inside a for loop. Right now I have it that upon holding down the mouse, the same movie clip will fall onto a ground object (and bounce up occasionally,) I'm trying to make it to so each ball could stand on top of each other. I've used collision detection before for separate objects, but never for instances of the same object inside a for loop.
View 0 Replies
Oct 14, 2011
I've attached a zip with a few custom classes and a .FLA. Ignore the custom classes. I'm getting repeated
"Error #1009: Cannot access a property or method of a null object reference. at Environment_fla::MainTimeline/hitTest()".
I understand that this error means I'm asking it to reference something that's no longer there. How better to hitTest many objects in an array. My game involves creating a bunch of balls that will fall into buckets. I need to keep track of how many touched the buckets, how many fell off the screen, and remove each accordingly.
Here is the code from my main FLA
ActionScript Code:
var ballTimer:Timer = new Timer(200);
var changeTimer:Timer = new Timer(50000);
ballTimer.addEventListener(TimerEvent.TIMER, throwBall, false, 0, true);
ballTimer.start();
changeTimer.addEventListener(TimerEvent.TIMER, changeColor);
changeTimer.start();
[Code] .....
View 6 Replies
Apr 2, 2011
[I apologise if this isn't really an in depth question, but I wanted to solve this once and for all]I was trying to get look into quadtrees, but already ran into trouble getting collision detection without any optimization working properly. Did a search and found a pretty neat example:Only some collisions are detected between particular objects. When the objects are not moving it seems to work alot better for some reason. really can't figure out whats the problem, the code is essentially the same as the sample. I did not blindy copy pasted it, I understands whats happening except Here is what I did:Main class
package
{
import com.martino.objects.Square;
[code]....
View 2 Replies
Mar 8, 2012
I am trying to test the collisions between a bullet and an array of enemies in Actionscript 2. However it is not sensing a collision.This is the code in the bullet.
onClipEvent(load)
{
facing = _root.player.facing;
speed = 1;
[code]....
View 1 Replies
Apr 23, 2011
how to work out the direction of the mouse on stage and say from this:
"if the mouse goes from the left to right (visa-versa) once, add score +1" and "if the mouse speed which is player controlled - slows down - to then display an error graphic"
View 21 Replies
Jul 29, 2010
Is there any as3 based source code that accomplishes pitch detection or beat detection accurately? Doesn't have to be both, if you know of at least one of those,
View 4 Replies
Dec 16, 2009
I am designing a webpage with a password (yes, I know it can be hacked)I want to be able have an alert appear in the textbox "mess" when the Caps Lock key is activated. (not just being held down). I have already tried this and it comes out as staying on until you leave the page or mess changes.[code]What can I put to make the "CAPS LOCK IS ON" go away when the capslock is off?
View 1 Replies
Jun 21, 2010
Im currently working on a demo flash game built on AS 2.0. im a very beginner to this flash and AS and im stuck up with a very silly concept though im not able to overcome it. the problem i have is with collision detection ie hit test and im really struggling with it.
[Code]....
View 1 Replies
Jul 19, 2009
I have an if statement set up for the rollout on a button:
on (rollOut) {
if (_root.mb == false) {
copymb.gotoAndStop('off'); } else if (copymb._currentframe ==
[code]....
View 2 Replies
Oct 30, 2010
I was wondering what do the AS3 experts would do to detect a color with the webcam (red) and draw a sprite on those color boundaries.
View 2 Replies
Mar 31, 2011
i have a snake game that uses a timer to incremement the snakes size, and ive been trying to figure out hit detection on it. and have got to this stage
for (var i = 1; i < snake.length; i++){ //this is where I am trying to make the hit
if (Math.floor(snake[0].x) == Math.floor(s.x) && Math.floor(snake[0].y) == Math.floor(s.y) ){
[code]....
View 1 Replies
Feb 17, 2010
I want to make a flash movie that detects OS in use, and if its Windows, go to a frame, if it's Mac OSX go to another frame, and if is another OS, go to other frame...
I know this is possible, but I don't know how to apply it.I know I can use System.capabilities.os but I don't know how!
View 2 Replies
Oct 16, 2010
Im using CDK, and I want to exclude color black.im using this: __collisionList.excludeColor(0x00000000);
View 5 Replies
May 3, 2009
Its been many years since I've used flash and I was just wondering what the easiest way to do collision detection was now. I know that there was some hit test bounding box stuff present earlier but I don't know the capabilities of flash cs4 and the advances that have been made. Google searches have yielded things like complex pixel-perfect collision detection but not a lot seems to have been written about CS4 yet.
View 3 Replies
Oct 11, 2005
automatically replacing a flash file with a jpeg, for non flash users.
I also am looking for some scrpt which tells users if they haven't got a/the correct flash plug in. My site uses a flash 5 file as well as flash mx 2004.
View 4 Replies
Mar 19, 2009
I am trying to get some collision detection working. What I have is a ball moving around the stage. There is a box in the middle which when the ball hits, I would like it to bounce off the box. I can get the x axis working fine using hitTestObject but when I detect for collisions on the y axis, the two axis conflict. Does anyone have a way of doing this properly?[code]
View 4 Replies
May 14, 2010
Is there any way to bypass the ugly Flash popup notice asking to allow mic or video access? We do not want to NOT ask for permission, but rather have our own visual design asking to allow access.
View 1 Replies
Feb 23, 2011
how difficult is it to create a flash game with a racing car picking up dots (movie clips) that give points?I managed to have the racing car moving and add something like wall boundaries but i cant find tutorials online that are clear about the colision detection...and the dynamic text field that scores points.
View 2 Replies
Aug 25, 2008
So I've been googling around for information on detecting collisions in AS3 and I found this page which has what troy claims to be a 'pixel-perfect' collision detection algorithm:[URL]..
[Code]...
View 1 Replies
Mar 25, 2010
1- i have a circular menu, with a zoom effect when you hover on it, with the home button in the center, when you click the home button you load the page then reload the scene 1 ( menu idle ).If i put nothing on the button itself on the scene 1, before the zoom effect, i need to move out of the button to trigger the other scritp there that detect the rollover and start the zoom in.i want to have a special behavior for the home button on the idle state of the menu ( scene 1 ) , When you click the home button from anywhere else, you load back the page, then because the mouse is already on the home button, i want flash to do nothing, but at the second you start to move the mouse again ( remember you are still on the home button) i want it to start teh zoom effect again, not before.
Right now, it is either no zoom effect as soon the mouse is on top of it, or imediatly zoom in with the rollover....And i know it is possible with a very simple command, but i forgot it... i did it before.2- for the same menu, i will have a sub-menu unfolding when you click a certain button, that sub-menu is outside the main menu detection zone. Is it possible to make a shape, turn the alpha at 0, put it under the sub-menu, and use it as detection zone
View 3 Replies
May 11, 2010
With AS, can I trace that user hasn't been operating on the computer for a while, say 5 miniutes, and then do something? theoretically speaking, to detect that there is no mouse or keyboard event for certain a period of time, and then do something~~ just like screensaver of Widows~
View 1 Replies
Oct 28, 2010
HitTestObject seems to check collision of bounddaries of shapes, it seems to consider everything rectangular. Also if one object jumps two pixel and other five pixels on EnterFrame it seems like there won't be any hit detection. Seriously guys what else method do you use, if you use hitTestObject how do you use?
View 1 Replies
May 4, 2011
I have 2 boxes in stage and I want to drag any where in free space but cannot through the boxes, if I drag box1 in the upward direction and collides to box2 , then box1 cann't be drag in upward direction but it can move(drag) any other three directions.
View 1 Replies
Apr 19, 2010
I'm making small interactive games in flash to learn AS3,I need to check the collision between the player and the wall which is normally simple using the hitTestObject function.But now I made a wall object totally surrounding the player with corridors and turn, a collision playground so to speak. Now when I use the hitTestObject function to check whether the player is in collision with the wall it tells me it always collides supposedly because the player object is within the bounds of the wall object.So assuming that I'm correct about the error:How can I prevent getting a collision when I'm inside the bounds of the wall object but not touching the actual walls in that object?
View 2 Replies
Aug 2, 2010
Is it possible to detect usb device using actionscript 3 from flash cs4?
View 1 Replies