Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
MDAndrewM
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Drag and Drop messing with .swf children

This is more of a flash based question, but it's one that has me baffled.

 

For anyone who has seen my previous posts, I am making a card game and you can select different card styles.  Some of these styles are animated (two to be exact).  These animated cards are in .swf format (a class extending movieclip has a loader in it that loads the .swf into it.

 

The first set of animated cards never has any trouble (it is a simple animation that is only two layers where something fades in on top of a background image).  However, the second one is giving me trouble once I start dragging and dropping in-game, problems arise.

 

When cards are dragged and dropped in my game, they are snapped, either back to their old location or to the new one if not placed directly on the spot.  I know that this is the issue, but I'm not entirely sure why it's doing what it's doing (don't worry I'm getting there)

 

In the .swf that I'm importing, there are other movie clips that move around on the card face (in circles).  Sometimes when I move the card, the card get's locked up in a funny place on screen (I have never had a problem with it not snapping to a correct place with any other card type) and some of the smaller movie clips get moved to a place outside of the card.

 

My question is, is there any way to prevent this in the way I am exporting my .swf from CS4, or do I need to use code (and what would the respective solution be).

 

I've already tried exporting the .swf as a series of images and importing it back in, but since the movie clips are what is animated, the standard animation in CS4 is only one frame long and thus it doesn't work.

 

Thanks in advance!

Please use plain text.
New Developer
daniel_wind
Posts: 56
Registered: ‎02-07-2011
My Device: BB Playbook
My Carrier: ---

Re: Drag and Drop messing with .swf children

Hi MDAndrewM,

 

I have some questions for you:

 

1. In Flash CS4, do you have each animated card as a single MovieClip containing all other necessary smaller MovieClips and proper animations OR do you have them split on the stage and you just publish the swf file?

 

2. This class that holds the Loader, are you accessing its children with "content" property?

 

3. Usually this method of loading content from Flash is not precisely the best way to do it. Are you experienced using Flash? Don't get me wrong, I ask just to be sure to give you possible solutions based on the answers you can provide.

 

That locking problem is generally due to loading swf files and not accessing their "content". As for the CS4 exporting hack, that is not going to work.  

 

 

-------------------------------------
www.danielwind.net

Playbook App:
Grab Da Crab
Please use plain text.
Developer
MDAndrewM
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Re: Drag and Drop messing with .swf children

in flash I have a single card as a number on the stage (text), a background rectangle with border and then the movie clips that are animated.  I publish with that on the stage.

 

My code for that class is as follows:

 

 

package
{
    
    import flash.display.Loader;
    import flash.display.MovieClip;
    import flash.net.URLRequest;
    
    public class Card extends MovieClip
    {
        public var suit:int,num:int,xCoord:int,yCoord:int;
        
        private var image:Loader;
        
        public function Card(s:int,n:int)
        {
            this.image = new Loader();
            this.addChild(this.image);
            this.suit = s;
            this.num = n;
            this.xCoord = -1;
            this.yCoord = -1;
        }
        public function move(x:int,y:int):void
        {
            this.xCoord=x;
            this.yCoord=y;
        }
        public function load(path:String):void{
            this.image.load(new URLRequest(path));
        }
        public function unload():void{
            this.image.unload();
        }
    }
}

 

 

 

i am not using the content property.  I've seen it, but i am not familiar with it enough to know how it could make a difference.

 

My action listener is being added to the card (in a loop):

 

this.cards[i].push(new Card(i,j))
addChild(cards[i][j]);
if (this.animatedCards){
     this.cards[i][j].load("Images\\Cards\\"+this.cardStyle+"\\"+i+"-"+j+".swf");
}else{
    this.cards[i][j].load("Images\\Cards\\"+this.cardStyle+"\\"+i+"-"+j+".png");
   }
   this.cards[i][j].addEventListener(MouseEvent.MOUSE_DOWN,dragCard);
   this.cards[i][j].addEventListener(MouseEvent.MOUSE_UP,noDragCard);


 

 

 

and the listener:

 

 

// listener when dragging card --------------------------------------------------------------------------------
        internal function dragCard(event:MouseEvent):void {
            if(_menu.isVisible()){
                _menu.hide();
            }else if (this.draggable){
                var drag:MovieClip = MovieClip(event.target.parent);
                this.setChildIndex(drag,this.numChildren-1);
                drag.startDrag();
            }
        }
        //-------------------------------------------------------------------------------------------------------------
        
        // listener when dropping card --------------------------------------------------------------------------------
        internal function noDragCard(event:MouseEvent):void {
            var drag:MovieClip = MovieClip(event.target.parent);
            var tempX:int = this.convertCoordToX(this.mouseX);
            var tempY:int = this.convertCoordToY(this.mouseY);
            if (viableMove(drag.suit,drag.num,tempX,tempY)){
                makeMove(drag.xCoord,drag.yCoord,tempX,tempY,drag.suit,drag.num)
                drag.x = this.convertXtoCoord(tempX);
                drag.y = this.convertYtoCoord(tempY);
                drag.xCoord = tempX;
                drag.yCoord = tempY;
            }else {
                drag.x = this.convertXtoCoord(drag.xCoord);
                drag.y = this.convertYtoCoord(drag.yCoord);
            }
            drag.stopDrag();
        }
        //-------------------------------------------------------------------------------------------------------------

 

 

 

 

 

I am not particularly experienced in flash.  I have done movies and such in the past (not great), but this is the first time I've used actual actionscript.

 

I hope that answers your questions, but if not, go ahead and ask more.

Please use plain text.
New Developer
daniel_wind
Posts: 56
Registered: ‎02-07-2011
My Device: BB Playbook
My Carrier: ---

Re: Drag and Drop messing with .swf children

Hi MDAndrewM,

 

Thanks for the provided information. When using the Loader Class, theSWF file or image loaded into the Loader object inherits the position, rotation, and scale properties of the parent display objects of the Loader object. 

 

However, if you have multiple"root" display objects on stage then it starts to act funny. My suggestions here:

 

1. Try to get all the card's components (which you mentioned you have on stage, text, background and small movieclips) and try to convert them all into one single MovieClip so there is only ONE root display object on the swf you are generating. 

 

2. Your code seem to be fine, however, I would suggest for you to cache (in some private var) the 

 this.image.load(new URLRequest(path));

for doing this only once (instead of several times during runtime execution, which I believe you are doing). 

 

3. Use "cacheAsBitmap" or "cacheAsBitmapMatrix" for smoother transition during dragging for these two cards exported from Flash. This will help your assets visually a lot.

 

4. Using this method of loading assets from flash is not really good in terms of final .bar size and processing affairs, specially if you are triggering it in runtime. My suggestion here is to generate a swc file from flash containing both cards (each previously exported to actionscript class), place a reference for the swc in flash builder actionscript build path, and creating a class extending each card class generated from flash cs4. If you want more info on doing this just let me know.

 

I hope this helps a bit.

 

 

 

 

-------------------------------------
www.danielwind.net

Playbook App:
Grab Da Crab
Please use plain text.
Developer
MDAndrewM
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Re: Drag and Drop messing with .swf children

I'm working on doing this, but since I'm not too familiar with flash, if you would explain what a swc is and why it might be better used here, that would be a great help.

 

Also, with your reference to chaching.  I am using different card faces (so i am calling the load when the user changes their card face), would this caching affect that and how exactly is it done?

 

Thanks a ton!

Please use plain text.
New Developer
daniel_wind
Posts: 56
Registered: ‎02-07-2011
My Device: BB Playbook
My Carrier: ---

Re: Drag and Drop messing with .swf children

@MDAndrewM,

 

Sure thing. OK so, a swc file is a component library file (like a .jar if you're java familiar) that contains classes and other assets that you generate in Flash IDE. It has many advantages over embedding assets on another swf ([Embed]) or loading swf files generated from flash.

 

1. You can add it to the build path so you have access to it such as you do with the QNX library.

 

2. You have access to each class reference that you define in Flash CS4/CS5 library, therefore the ability to extend them in other framework/environment (Flex Framework).

 

3. Your result bar file gets reduced. You are not required to create any assets folder in your project as you have them available in the swc (you can set images, movieclips, sprites, etc). I have been working on flash since many years ago and this technique proves to be the best for resulting file reduction. I use it in the apps I have pushed to the appworld.

 

In reference to caching, just load both swfs at determined moment, create a private var for each and access each swf's root displayobject and assign it to that var like this:

 

private static var somevar:smileyvery-happy:isplayObject

///rest of code here//

 

//then after loading the swf//

somevar = loaderinstance.content;

 

**Then it will be available in all your app (just cast it to MovieClip)

 

I hope this helps. I'll be reading your comments but will comment at night as my wife is bugging me :smileyvery-happy:

 

Thanks,

-------------------------------------
www.danielwind.net

Playbook App:
Grab Da Crab
Please use plain text.
Developer
MDAndrewM
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Re: Drag and Drop messing with .swf children

I chached all my cards and backgrounds, which makes it run a lot faster (thanks!) when switching cards.  When it comes to the .swc files, I've been looking around the internet to find a good example of how to use them, but I am not having any luck.

 

If you could maybe walk me through how I'd access it, that would be awesome.  let's say i output the .swc of a card which is a movie clip (can i get multiple movieclips into an swc? I tried, but couldn't figure out if i could) called 2ofspades.swc.  How do i add that to my buildpath and how would i access the movieclip from that package to access the image?

 

I know this might be a bit tedious (and sorry for getting you in trouble with the wife), but getting specifics saves me a ton of time (and i am experimenting in the mean time as i wait).  Your help has been invaluable thus far.

Please use plain text.
New Developer
daniel_wind
Posts: 56
Registered: ‎02-07-2011
My Device: BB Playbook
My Carrier: ---

Re: Drag and Drop messing with .swf children

Hi MDAndrewM,

 

Glad to hear things are working better for you! For working the swc file:

 

1. Go to Flash CS4 (or CS5 does not matter) and try to convert your card stuff to one single movieclip. Delete it from the main scene (the flash "canvas"). You will have the reference to that movieclip on the assets library.

 

2. On the library, locate that movieclip and right click on on it, select "properties" and in the popup, select "export to actionscript".  Set any name you want for the class and click ok.

 

3. Try adding the other card (which I suppose you have on other FLA, if it is on the same one then great) into one movieclip too. (Let's say one mc called card1 and the other one card2).

 

4. Go to publish settings, select the flash tab and in the "swf settings" click on "Export SWC". This will generate a swc file when you publish (you'll get the swf too).

 

5. Publish the swf (as you do normally). You will see a swc file generated that will contain everything you have in your assets library (previously exported classes and any other assets).

 

6. Now, open your flash builder project, create a new directory anywhere within your project's src directory (I usually create a directory called "libs" under src, you can call it whatever you want).

 

7. Place the generated swc in there. 

 

8.  Select the main project folder (root) and right click on it, select "properties"

 

9.  From the left select Actionscript Build Path and then select (on the right) "Add swc". Look for your swc within your project and add it. Then click OK.

 

10. Now you have your movieclip completely accesible from code. Just call it from the name class you entered when you did set the properties in Flash CS4.

 

I have zipped some screenshots showing the process. You can access them from my website here: 

http://www.danielwind.net/downloads/SWCDEMO.zip

 

Hope this helps you.

Thanks,

 

 

 

 

-------------------------------------
www.danielwind.net

Playbook App:
Grab Da Crab
Please use plain text.
Developer
MDAndrewM
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Re: Drag and Drop messing with .swf children

This will break my loops that fill the arrays of cards, but it will make my life a heck of a lot easier.  Thank you very much!

Please use plain text.
Developer
MDAndrewM
Posts: 32
Registered: ‎02-22-2011
My Device: Not Specified

Re: Drag and Drop messing with .swf children

Okay, I'm having a weird issue.  Since i have to compile my actually app via command line, I made a new project in Flashbuilder to test my .swc.  It worked perfectly.

 

 

package
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	
	import qnx.ui.buttons.Button;
	import qnx.ui.display.Image;

	[SWF(width="1024", height="600", backgroundColor="#cccccc", frameRate="30")]
	public class TestSWC extends Sprite
	{
		public function TestSWC(){
			var testS:AtomSpadesTwo = new AtomSpadesTwo();
			testS.x=512;
			testS.y=300;
			this.addChild(testS);
			
		}
	}
}

 

 

 

however, when i tried to add it to my own project, I ran into problems.

 

 

package
{
	
	
	import flash.display.MovieClip;
	
	public class SWCCard extends MovieClip
	{
		public var suit:int,num:int,xCoord:int,yCoord:int;
		
		public function SWCCard(s:int,n:int)
		{
			
			var image:AtomSpadesTwo = new AtomSpadesTwo();
			this.addChild(image);
			this.suit = s;
			this.num = n;
			this.xCoord = -1;
			this.yCoord = -1;
			
		}
		public function move(x:int,y:int):void
		{
			this.xCoord=x;
			this.yCoord=y;
		}
	}
}

 This is another class in my project (not the main one) which just keeps track of it's location for some of my other coding stuff.  When i try to compile it, though it says:

 

 

...\src\SWCCard.as(15): col:
 34 Error: Call to a possibly undefined method AtomSpadesTwo.

                        var image:AtomSpadesTwo = new AtomSpadesTwo();

 

It definitely exists (autocomplete finishes it).  Do you know what might be causing this?

 

 

Please use plain text.