View Full Version : creating class with external image.
Stonehambey
May 25th, 2008, 07:30 PM
Hi,
I know that I can import an image to the library and then export it for actionscript, but then I'm unable to edit the implementation of the class (I think, if there is any way then please let me know)
So I create a class from scratch and instead of using the graphics class to make a ball or square or whatever, I would like to use an external image. How do I do this?
I've tried loading URLs but I just can't seem to get it to work for OOP style AS project. Only in the main document class, which is useless to me :P
If anyone could help I would be very grateful,
Cheers,
Stonehambey
Anogar
May 25th, 2008, 07:50 PM
Actually, if you make a symbol in your library containing the bitmap (lets say it's called Image) you can make a class called Image.as and it'll be run whenever you instantiate a new instance of that image. (var myImage:Image = new Image())
Stonehambey
May 25th, 2008, 07:57 PM
Oh that's interesting, do I still have to export it for actionscript if I do it that way?
Thanks for the reply :)
Stonehambey
May 25th, 2008, 08:05 PM
eek, I got this error when I tried it
5000: The class 'Bloke' must subclass 'flash.display.BitmapData' since it is linked to a library symbol of that type.
My class and image are called "Bloke" ^_^
amarghosh
May 26th, 2008, 03:34 AM
import flash.display.BitmapData;
public class Bloke extends BitmapData
{
}
sparkdemon
May 26th, 2008, 04:57 AM
Or you can just convert it to MovieClip, then export for actionscript
import flash.display.MovieClip;
public class Bloke extends MovieClip
{
}
provides more facility to work on the object :)
Stonehambey
May 26th, 2008, 07:48 AM
Thanks for the replies guys, I think I'm almost there, only one error now. I'll post the code I'm using so you can see what's happening in full and hopefully where I'm going wrong.
So I have a main flash document (Untitled.fla) with a blank page and an image in the library called "Bloke". At the moment he's not exported for actionscript, but I'm getting the same error whether he is or isn't, so I don't think the error has anything to do with that.
The document class is Bloke_test, which is this.
package
{
import flash.display.*;
public class Bloke_test extends Sprite
{
public function Bloke_test()
{
init();
}
private function init():void
{
var bloke:Bloke = new Bloke();
bloke.x = stage.stageWidth/2;
bloke.y = stage.stageHeight/2;
addChild(bloke);
}
}
}Then I have a bloke class which looks like this
package
{
import flash.display.*;
public class Bloke extends BitmapData
{
public function Bloke()
{
}
}
}When I test the movie I see the following error
1203: No default constructor found in base class flash.display:BitmapData.I'm a bit stumped on this one, I'm still pretty new to AS3. Any help would be much appreciated thanks! :)
amarghosh
May 26th, 2008, 07:51 AM
oops--- am sorry. if u extend BitmapData class it will call its super constructor by default and it will create problems as it expects width and height etc... go with sparkdemon's solution. that would be better;
Stonehambey
May 26th, 2008, 08:10 AM
Thanks, it works now :)
In the future I want to have a single sprite or movieclip class, which returns a different image depending on what the user has clicked in a dropdown menu. Instead of having like 100 different classes for each clip I thought it would be easier to have one class which loads a different image depending on what the user wanted.
I have a feeling that will not work using this method but that is a way in the future and I'll cross that bridge when I come to it. For now, thanks for all the help guys :)
Jop
November 26th, 2008, 07:11 AM
What you need to do is pass the width and height to 'Bloke' class that extends the BitmapData class. Like this:
package{
import flash.display.BitmapData;
public class Bloke extends BitmapData {
public function Bloke(_w:Number, _h:Number){
super(_w, _h);
}
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.