[AS3] MouseOverButton reseting tiles

10 posts

Flag Post

Hi,
I’m new in Flash and AS3, but wanna make game in this language (in future more games I hope), made some on WP7 (low games) but I don’t know why, it’s not that I want to do in my free time. So now I’m making big game for flash.

There’s first question which I can’t find answer on google.
I made some classes:
DocumentClass with navigation system and all things handling all game.
Then there is GameClass which contains all mechanism only during play.
In GameClass I’m creating map of tiles 10×10. Till now it’s not even an array, but making those tiles to check if they’re working properly.. and NO! :(
I have TileSweeper movie clip and two others movie clips like: UncoveredTileSweeper and CoveredTileSweeper (cause dont know how to change them in class from one jpg to another, so I’m just adding new child and removing old one after clicking each tile) that works!

there’s something like this with “extending” movie clips and buttons:
Game <- TileSweeper <- CoveredTile
^-MenuButton ^- UncoveredTile
Is it readeable? :P

The thing is, I have a button “Menu” in this Game stage, and when my mouse is over the button, all tiles are genereating from the beginning so again all are covered. I’ve read somewhere that’s because when mouse is over the button, flash’s going to frame number 2 (somewhere???)and that’s why Game Class is reseting. But couldn’t find answer how to stop it?

Sorry maybe for mixing up all those names like stage, or using those names in wrong context, juxt in this thing from 2 weeks maybe and i’m learning on my own, so don’t know if im making some stupid mistakes and there’s noone to correct me.

If u don’t understand I’ll try to explain it with images, but rly need help with that. Cause no idea how to fix it.

package  {
	
	import flash.display.MovieClip;
	import flash.events.*;
	
	
	public class Game extends MovieClip {
		
		private var _root:MovieClip;
		public function Game() {
			this.addEventListener(Event.ADDED, beginClass);
		}
		
		private function beginClass(e:Event):void{
			_root = MovieClip(root);
			menuButton.addEventListener(MouseEvent.CLICK, onClickMenu);
			putTiles();
		}
		
		private function onClickMenu(event:MouseEvent):void{
			dispatchEvent(new NavigationEvent(NavigationEvent.MENU));
		}
		
		private function eFrameEvents(e:Event):void{
		}
		
		private function putTiles():void{
			var xPos:int = 395;
			var yPos:int = 45;
			for(var i:int = 0; i < 10; i++){
				for(var j:int = 0; j < 10; j++){
					var tile:TileSweeper = new TileSweeper();
					tile.x = xPos + j*33;
					tile.y = yPos + i*33;
					_root.addChild(tile);
				}
			}
		}
	}
	
}
package  {
	
	import flash.display.MovieClip;
	import flash.ui.Mouse;
	import flash.events.MouseEvent;
	
	public class TileSweeper extends MovieClip {
		
		private var coveredTileSweeper:CoveredTileSweeper;
		private var unCoveredTileSweeper:UnCoveredTileSweeper;
		
		public function TileSweeper() {
			addEventListener(MouseEvent.CLICK, onClickTile);
			coveredTileSweeper = new CoveredTileSweeper();
			addChild(coveredTileSweeper);
		}
		
		private function onClickTile(Event:MouseEvent):void{
			unCoveredTileSweeper = new UnCoveredTileSweeper();
			addChild(unCoveredTileSweeper);
			removeChild(coveredTileSweeper);
			coveredTileSweeper = null;
			removeEventListener(MouseEvent.CLICK, onClickTile);
		}
	}
}

megaswf.com/file/2470667 (it’s this game, can’t attach link cause need more post, but I think it’d be very hepful to this case. And dont know why it’s so small, there’s button to full screen anyway)
Just click Start Game then click at least one tile, and then go with the mouse over the button “Menu” Tiles covered again. Why

 
Flag Post

I found that with debugger:

public function Game() {
this.addEventListener(Event.ADDED, beginClass);
}

private function beginClass(e:Event):void{
_root = MovieClip(root);
menuButton.addEventListener(MouseEvent.CLICK, onClickMenu);
putTiles();
}
the beginClass are running again after my mouse is OVER the menu button which is added to this MC by dragging it in Game.
Which is I guess pretty obvious when I have it all cleared again. But now it’s much more clear to see for you guys :)
Should I do something with layers in timeline and button and all game? I’ve put already button into another layer, trying stop() function hopeful it’s gonna work, but nah.
I think I don’t understand all things in flash but please let me know what I don’t know.

 
Flag Post

You shouldn’t be using frames or the timeline at all.

on your beginClass method add the following: removeEventListener(Event.ADDED, beginClass);

You must always clear listeners that you won’t use anymore.

About changing the image, a Bitmap would do the trick: you’ll cache the bitmapData of bot images and when you want to change the graphic, you’d simply set the bitmapData of your bitmap to the new graphic.

Also, if an object won’t have a timeline (frames), use Sprite instead of MovieClip.

E: Also, in my opinion it’s better to use sprites instead of buttons (as in SimpleButton).

 
Flag Post

Thank you very much, will try after getting some achivements ;P
Silly me, forgot to clear listener, but thank you for those ideas with sprites. Can u tell me benefits of using sprites? Optimization? Or only more handly to code?
Can I ask how to embed jpg/png from directory to make this graphic? [Embed(source=“player.jpg”)] var player:class; ? Like this? cause was trying latly but had some problems, but didn’t know it’s for bitmapData, and then use var player to set the bitmapData?

 
Flag Post

Embedding depends on the software you use. Seeing as you use frames and MovieClips, I expect you are using Flash CS5 or other CS, regardless. There you have a “library” where you could put JPGs as well as other pictures, graphics objects, either hand-drawn or imported, etc, and they have properties and can be “exported for Actionscript”. This lets you name each of the resources with a name of the class, say “Player” for a JPG will make a “Player” class descendant of Bitmap. Then you can use them in your code, like var the_player:Bitmap=new Player();

 
Flag Post

About using sprites instead of buttons, I find them easier to control and work with. For example, in your case, I don’t know why on earth mousing over a button would cause all of that, when you’re only adding a click listener to it; mouse over shouldn’t be triggering a single thing.

And yes, the Embed metatag is the way to go, but note that it’s part of Flex, so if you are using CS, you’ll have to change its settings and point it to the Flex folder.

Example of changing the image:

// Assets.as
[Embed(source="../lib/graphics/pic1.png")]public static const PIC1:Class;
[Embed(source="../lib/graphics/pic2.png")]public static const PIC2:Class;
//

class Test extends Sprite...
private var pic:Bitmap=new Bitmap(new BitmapData(1,1));  
// the container for all the images. You can use a default image instead of what I did.
private const DATA:Vector.<BitmapData>=new <BitmapData>[
new Assets.PIC1().bitmapData,
new Assets.PIC2().bitmapData    
];  // store all the information of the images. This will use more memory but avoid the
temporary creation of new objects. Using this or not depends on how often you'll be
changing the images.
...
...
addChild(pic); 
...
...
something function setImage(value:int):void{
pic.bitmapData=DATA[value]; 
// and that alone would change the displayed image.
}

Also, something I forgot to mention in my previous post: Don’t use root. I’d even go against using stage for adding display objects.
The instance of your game class is already on the stage: simply addChild(stuff), without the _root thing.

A good application commits suicide everytime someone tries to emulate AS2 in AS3. </3

 
Flag Post

Root thing is taken from one tutorial how to make your tower defense game AS3, and yes, guy was teaching in AS2 and AS3, so guess he took some habits from AS2 to AS3.
And I met problem with it, because in one line I had to use _root.addChild(…) because my app frozen other way, but think it’s the bigger problem in earlier stage. So now I’m going to change almost everything from this movie clips to those sprites.
Thank you once again. I’m positive surprised how this community on kongregate is helpful. Will try bother u as little as possible, but now I know where I can search for help.

If i can change topic one time. If i make game with some animations, and have access to flash CS 5.5, ofc the most things will be coded but with some more complicated animations like fighting animation. Should I use Flash CS or FlashDevelop (still haven’t installed it yet so no idea how it looks like), cause I found somewhere it’s better for coding but for animations is better flashCS (true? nah?) , should I somehow mix it or stick with one?

Best regards.

 
Flag Post

Yes, CS is good software for making animations and FD is better for the coding aspect, mixing them sounds like the way to go in this case. Another option would be using spritesheets (you have a single image with all the “frames” of your animation and only draw the rectangle containing the required frame), or you can use MovieClips in CS. Then you’ll have to export a SWC with all your animations and import that SWC into FD, then you can grab them from there.

 
Flag Post

Thx for the opinion and advice, already working out this new tool.
That’s all for the thread ;) Log out.

 
Flag Post
Originally posted by Senekis93:

I’d even go against using stage for adding display objects.

Not only you.

Even the docs say so…