An OO revision

55 views
Skip to first unread message

Jens Heldt

unread,
Dec 17, 2012, 12:32:55 PM12/17/12
to haxe...@googlegroups.com
Im trying to alter the source code that was given to me on the Haxeforums. I wanted the constructor to allow parameters:


import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.net.URLRequest;


class Tiles extends Sprite
{
    static var loader = new Loader();
   
   
    public static function load () {
        loader.load(new URLRequest("tiles.png"));
        }
   
   
    public function new(tilex:Int,tiley:Int)
    {
        super();
        var content:Bitmap = cast(loader.content, Bitmap);
        var source:BitmapData = content.bitmapData;
        var sourceRect = new Rectangle(20,0,20,20);
        var destPoint = new Point(0,0);
        var bmd = new BitmapData(20,20);
        bmd.copyPixels(source, sourceRect, destPoint);
        var bmp = new Bitmap(bmd);
        bmp.y = tilex;
        bmp.x = tiley;
        addChild(bmp);
       
       
    }
 public static function main()
    {
        load();
        var Tile = new Tiles(320, 0);
       
       flash.Lib.current.addChild(Tile);
       
    }
}

And when I compile it, it gives no errors. Yet when I view the flash window, the tile hasn't been added. Can anyone locate the error I've made? I've checked multiple that the "tiles" image is in the correct directory. 

Franco Ponticelli

unread,
Dec 17, 2012, 12:44:46 PM12/17/12
to haxe...@googlegroups.com
Since Loader is asynchronous I suspect that you are using loader.content too soon when it has not been loaded yet (so a blank image).

Franco


Jan_Flanders

unread,
Dec 17, 2012, 1:56:09 PM12/17/12
to haxe...@googlegroups.com


On Monday, December 17, 2012 6:44:46 PM UTC+1, Franco Ponticelli wrote:
Since Loader is asynchronous I suspect that you are using loader.content too soon when it has not been loaded yet (so a blank image).

Franco

What Franco says. :)
Please use the example as it was provided, or at least don't just remove parts of the code when you don't know what they were doing. ;)
http://haxe.org/forum/thread/4458
Store the parameters in the constructor and use them in the onLoadComplete function.

Jan

Jens Heldt

unread,
Dec 17, 2012, 2:33:26 PM12/17/12
to haxe...@googlegroups.com
But wont it reload the tiles image, everytime I create a new object that uses that class? remember I'm quite new to OOP

Also, I couldn't figure out how to make the "Onloadcomplete" function accept parameters, or how to pass parameters into it.

Baluta Cristian

unread,
Dec 17, 2012, 3:40:31 PM12/17/12
to haxe...@googlegroups.com
The event will take only one parameter which has some useful properties like 'target'. However if you keep a reference to the loader you can use that.

You don't have to work with statics, you can instantiate Tiles right from the beginning and load the image in the constructor. After the image is loaded you instantiate another class called Tile which will get as parameters the Bitmap and a Rectangle where to crop from. If i understand what you're trying to do, setting the x and y of your new bitmap doesn't mean you've cropped the tile that begins at x and y.

Btw, you're using some wrong programming conventions. variables and functions should begin with lowercase.


--



--
Băluță Cristian
http://ralcr.com
http://imagin.ro

Jan_Flanders

unread,
Dec 17, 2012, 7:40:20 PM12/17/12
to haxe...@googlegroups.com


On Monday, December 17, 2012 8:33:26 PM UTC+1, Jens Heldt wrote:
But wont it reload the tiles image, everytime I create a new object that uses that class? remember I'm quite new to OOP

Also, I couldn't figure out how to make the "Onloadcomplete" function accept parameters, or how to pass parameters into it. 

Jens Heldt

unread,
Dec 18, 2012, 6:26:23 AM12/18/12
to haxe...@googlegroups.com
Jan I'll keep those pastes handy, but I've actually managed to get it to work:
http://pastebin.com/hTK0upMR

Sure I should get rid of a few of those middleman functions, but for now it will work.
Reply all
Reply to author
Forward
0 new messages