[AS3] Collision Problem

18 posts

Flag Post

I have two variables created in different functions and I want to see if they collide using hitTestObject. However, they are both local variables and I don’t know how to access them both in the same function. Any help?

 
Flag Post
Originally posted by Mysterymason:

I have two variables created in different functions and I want to see if they collide using hitTestObject. However, they are both local variables and I don’t know how to access them both in the same function. Any help?

What? You don’t collide variables (not in the way you’re talking about anyway), you collide physical objects. If you’re working in Adobe CSX that means two movieclips. You are the one who gives the movieclips their names so it shouldn’t be hard to figure out how to access them.

 
Flag Post

OK, that makes more sense. Still, when I do that I get the error:

1061: Call to a possibly undefined method hitTestObject through a reference with static type Class.

 
Flag Post

you can store them publicly in the scope of the class instead of in the function. That way you can access them anywhere in that class instead of only in the function. That’s probably the simplest way.

 
Flag Post
Originally posted by ItsaMeeeeeMario:

you can store them publicly in the scope of the class instead of in the function. That way you can access them anywhere in that class instead of only in the function. That’s probably the simplest way.

Please explain. Noob here.

 
Flag Post
package{

public class Stuff{
    //class scope 
      private var something:String;
      public const VERSION:int=1;
      // etc, etc.

public function Stuff():void{
something="Hello!";
somethingElse("Bye");
}

public function whatever():void{
    // function scope
    var text:String="Hi, there.";
}

public function somethingElse(data:String):void{
     trace(text); // error, since text is a value only accessible inside of the whatever function.
     trace(something); // works, since something is a value accessible to the entire class.
     trace(data); // works, since data is accessible to this method.
}

}

}
 
Flag Post

^ Those are helpful in the sense that they teach about scope, but they aren’t really dealing with his wish to be able to collide two objects.

OP: context is important, you need to describe where you are putting the code you are using because it matters a lot. If you run the same code in one object as in another object, or in a frame for that matter it’s almost certainly going to not work right.

So, where are you placing this code? Also, if you could post what code you have written that would also help.

 
Flag Post

Well this may help? Let me know. I don’t mind clarifying more.

package{

  import flash.display.MovieClip;

    public class Main extends MovieClip{

	public var globalObject1:MovieClip;  // can reference these vars anywhere in this class in any function
	public var globalObject2:MovieClip;  // declare global vars outside of functions to use anywhere

	public function Main(){
	  var localObject1:MovieClip;  //can only reference this var in this function.
	  var localObject2:MovieClip;

	  var myMovieClip1:MovieClip = new Square();  //pretending you have a MC in your library named square.
	  var myMovieClip2:MovieClip = new Square();  //making 2 of them to hit test

	  localObject1 = myMovieClip1;  //setting the squares to the vars declared inside this function
	  localObject2 = myMovieClip2;

	  globalObject1 = myMovieClip1;  //also setting them to the global vars declared outside of the function
	  globalObject2 = myMovieClip2;
	}

	public function tryHitTest(){
	  if( localObject1.hitTestObject(localObject2)){  //this will give you an error because the 2 vars used arent in scope
	    doCrap();
	  }
	  if(globalObject1.hitTestObject(globalObject2)){  //this WILL work since you can access global vars anywhere.
	    doCrap();
	  }
       }
    }
}
 
Flag Post

Hmm, I am putting all my code in the main Actions page on Frame 1. Is this wrong? I’m trying to store a function in the Player class and refer to it by Player.shoot() but it is not working. Could someone explain how classes work?

 
Flag Post

Yes, it is wrong.

Your function isn’t working because you’re trying to reference it as a static one.
Instead of doing Player.function(), create an instance of Player: private var player:Player=new Player();, then do player.function();

 
Flag Post

OK, I have done that and there are no errors. However, my bullets are messing up. Here is the function:

public function shoot():void
{
trace(“shoot”);
var newBullet = new Bullet();
addChild(newBullet);
newBullet.x = player.x;
newBullet.y = player.y;
var bulletTween:Tween = new Tween(newBullet, “y”, None.easeNone, newBullet.y, -5, 0.5, true);
}

 
Flag Post

Where is that function located?
How are they messing up?

 
Flag Post

Frame 1 actions.

Player class.

Enemy class.

The shoot worked but really messed up on the bullet positioning and I couldn’t change it. The newEnemy function doesn’t work at all though.

 
Flag Post

There are so many errors…
I think you should follow this tutorial to learn, then you should rebuild this project from scratch once you have a solid understanding of the basics.

 
Flag Post
Originally posted by Senekis93:

There are so many errors…
I think you should follow this tutorial to learn, then you should rebuild this project from scratch once you have a solid understanding of the basics.

Thanks for the link, I appreciate it!

 
Flag Post
Originally posted by Mysterymason:
Originally posted by Senekis93:

There are so many errors…
I think you should follow this tutorial to learn, then you should rebuild this project from scratch once you have a solid understanding of the basics.

Thanks for the link, I appreciate it!

For future reference, while posting your full code is good, ideally you want to copy and past it between [pre] [/pre] (the brackets are really ‘<>’ style) tags instead of using screenshots. That way, people can copy and paste your code and make modifications easier.

 
Flag Post

Do you actually need to import any classes if you’re writing something on a frame?

 
Flag Post
Originally posted by feartehstickman:

Do you actually need to import any classes if you’re writing something on a frame?

I think all (or at least many) of the flash.* packages are imported automatically at compilation. There’s a configuration file (Adobe Flash CSx/Common/Configuration/ActionScript 3.0/implicitImports.xml) which allows you to see/set which packages are imported.