Interacting with other MovieClips in AS3 (Solved)

14 posts

Flag Post

Hey there. So, I’m new to AS3, and I’m having a hard time with interaction. Basically, I wanted a stamina bar for my character that would deplete while sprinting. I’ve figured this out, but it kind of feels off to me. This is what I’ve done:

I created the stamina bar and converted it to a MovieClip (called “Stamina”). I then attached the base of an AS3 Class file to it. Then in my player’s class, I used this code:

“var staminaBar:Stamina = new Stamina();
stage.addChild(staminaBar);”

and this allowed me to interact just fine with it (as in, adjusting the width with “staminaBar.width = (stamina/staminaMax):”), but I feel that I’m doing something wrong because I had to attach an AS3 file with absolutely no functions in it whatsoever to get it to work. Maybe that’s right and I just have to get used to it. Now the real problem enters:

How do I add walls? I could use the above method, but I would have to place all of the blocks in code and that could make it extremely difficult to design complex levels. I was wondering if there’s a way to design the levels on the stage and just have other MovieClips interact with them? If anyone could help, that’d be greatly appreciated. If I need to clarify on something, just let me know.

 
Flag Post

You’re doing good so far. you just need to add a function called updateStamina, so your stamina bar will show the proper amount you currently have.

its pretty much
this.gotoandstop(currentStamina);

 
Flag Post

Thanks for the reply, FlashGrenade. As of right now, I’m adjusting the width of the bar based on the stamina. I’ll have to remember the technique you mentioned. As for designing the levels, does it absolutely have to be done in code, or can I place the blocks of walls in the stage and I’m just missing something on interacting with them?

 
Flag Post

If you are talking about designing the levels using Flash Pro’s drag and drop interface, you can recognise them without giving an instance name by using the getChildAt(index) method.

 
Flag Post

if you’re designing a 2d, you could design a simple level using the flash drwaing tools inside a movieclip, give names to the movieclip and the walls it self.

For instance, you create an empty movieClip named: mcLevelOne
Inside you draw a square named: mcSquare

You need to have the square instatiated so you can access it once you add it (to check for collisions).
So let’s say the level 1 would be just touch the border of the square, then you would addChild(mcLevelOne) and once that’s done you would access it through, mcLevelOne.mcSquare and use test colision. Hope i was of help, good night ;)

 
Flag Post
Originally posted by Gantar180:

but I feel that I’m doing something wrong because I had to attach an AS3 file with absolutely no functions in it whatsoever to get it to work.

Nope, that’s how it works. Any object that is creatable in that fashion (that is, you can get a copy from the library with code) is required to have a “blank class” in order to work.

 
Flag Post

Thanks for the replies guys. I was still confused after reading the comments, but I’ve been trying to use what I could make out from the comments, while reading the reference guide and doing some searches. This is what I came up with:

I created a blank MovieClip called “mc_levelOne”, and I then designed the level inside of that. After that, I used the frame’s actions and put in this code:

var level:mc_levelOne = new mc_levelOne();
addChild(level);
level.x = 0;
level.y = 0;

After that, I put this inside of the Player’s class for collisions:

if (hitTestObject(MovieClip(root).level) {
  //Collision code
}

Either way, collisions are now working, and I’m pretty sure this will work fine for multiple levels, I’ve just got to add the creation code for each level inside of the appropriate frame’s actions. Thought I’d share just in case anyone else is having problems. Thanks again, guys!

 
Flag Post
Originally posted by Gantar180:

After that, I put this inside of the Player’s class for collisions:

if (hitTestObject(MovieClip(root).level) {
  //Collision code
}

MovieClip(root) is ugly.
Do your hit detection in the document class where you have references to all objects that you want to test.

 
Flag Post

Hey there, NineFiveThree. I actually didn’t know about document classes. I looked into it, and even tried creating a small game where enemies spawn from the top and you have to avoid them, and I’m using a document class for it. It seems like it could make things a lot easier, but I’m still having a problem. Here’s the code from the document class:

package  {
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.ui.*;
	import flash.utils.Timer;
	import flashx.textLayout.container.ContainerController;
	
	
	public class Main extends MovieClip {
		//Hide mouse and declare public variables for paused boolean and enemy and container reference.
		Mouse.hide();
		public static var isPaused:Boolean = false;
		public static var enemy:Skull = new Skull();
		public static var container:Container = new Container();
		
		public function Main() {
			//Add container, healhbar, player, and a timer for spawning enemies.
			stage.addChild(container);
			var player:Player = new Player();
			stage.addChild(player);
			var healthBar:HealthBar = new HealthBar();
			stage.addChild(healthBar);
			healthBar.x = 0;
			healthBar.y = 0;
			var myTimer:Timer = new Timer(Math.floor(Math.random() * 400)+30,0);
			
			//Check for keypress to pause game.
			function KeyPressed(evt:KeyboardEvent):void {
				switch (evt.keyCode) {
					case Keyboard.P:
						isPaused = !isPaused;
						if (isPaused == true) {
							Mouse.show();
							myTimer.stop();
						} else {
							Mouse.hide();
							myTimer.start();
						}
						break;
				}
			}
			
			//Creates enemy.
			function DispatchEnemy(evt:TimerEvent):void {
				enemy = new Skull();
				container.addChild(enemy);
				enemy.x = Math.round((Math.random()*550)+1);
				enemy.y = -80;
				myTimer.delay = Math.floor(Math.random() * 500)+30;
			}
			
			//Adjusts frame of healthbar.
			function OnLoop(evt:Event):void {
				healthBar.gotoAndStop(Player.playerHealth);
			}
			
			//Add event listeners for main, key presses, and spawning enemies, and starts the timer.
			stage.addEventListener(Event.ENTER_FRAME,OnLoop);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
			myTimer.addEventListener(TimerEvent.TIMER,DispatchEnemy);
			myTimer.start();
		}
	}
}

The problem is, I get this in the output:
“TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Main()”

I have no idea what that means. I’ve tried looking it up, and most people actually get a line of the code to look at for the error. However, it seems that it may have something to do with event listeners, judging from the amount of people responding saying that it’s caused from event listeners. However, if I just remove

var player:Player = new Player();
stage.addChild(player);

And then add the player to the stage through Flash Pro, then everything works fine. Anyone know what’s causing it? Any help is much appreciated.

 
Flag Post

I think the problem may be in your player constructor. Are you accesing the stage there?

 
Flag Post
Originally posted by Gantar180:


The problem is, I get this in the output:
“TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Player()
at Main()”

I have no idea what that means. I’ve tried looking it up, and most people actually get a line of the code to look at for the error. However, it seems that it may have something to do with event listeners, judging from the amount of people responding saying that it’s caused from event listeners. However, if I just remove

var player:Player = new Player();
stage.addChild(player);

And then add the player to the stage through Flash Pro, then everything works fine. Anyone know what’s causing it? Any help is much appreciated.

The correct code for whatever needs stage to properly work:

public function Player(){
    if (stage) init();
    else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event=null):void {
    removeEventListener(Event.ADDED_TO_STAGE,init);
    // rest of the constructor code goes here
}
 
Flag Post

Thanks guys. Vesperbot, the code you provided fixed the problem. My small little game is fully functional now! Thanks again, everyone has been very helpful.

 
Flag Post
Originally posted by Gantar180:

I have no idea what that means. I’ve tried looking it up, and most people actually get a line of the code to look at for the error.

Either check the “permit debugging” box under publish settings, or press ctrl+shift+Enter to run it in debug mode. (Or use FlashDevelop, which makes this even easier – just select “debug” in the toolbar and leave it until the final version.)

 
Flag Post

Wow, someone new to AS3 who is actually doing things correctly. Congratulations sir!