1 decade ago by mattk07
Hi all,
I've been trying to figure out a way to create a simple entity that I can add to weltmeister that will load the an image, and resize the collision box to the image size. This is simply going to be used as a placeholder for the real graphic that gets loaded, it saves me creating a custom entity for each image.
The problem I'm facing is that with the following code, the image will load fine in the weltmeister editor, but when used in the real game itself, is scaled up twice the original scale. For example, a 100px X 100px image gets scaled up to 400px X 400px in the game, but it diplays perfectly fine in the WM editor. Not sure whats going on, any help would be greatly appreciated. Also if someone has a better way of achieving what I'm doing I would appreciate that too.
I've been trying to figure out a way to create a simple entity that I can add to weltmeister that will load the an image, and resize the collision box to the image size. This is simply going to be used as a placeholder for the real graphic that gets loaded, it saves me creating a custom entity for each image.
The problem I'm facing is that with the following code, the image will load fine in the weltmeister editor, but when used in the real game itself, is scaled up twice the original scale. For example, a 100px X 100px image gets scaled up to 400px X 400px in the game, but it diplays perfectly fine in the WM editor. Not sure whats going on, any help would be greatly appreciated. Also if someone has a better way of achieving what I'm doing I would appreciate that too.
ig.module( 'game.entities.building' ) .requires( 'impact.entity' ) .defines(function(){ EntityBuilding = ig.Entity.extend({ size: {x: 10, y:10}, // collision box size offset: {x:0, y: 0}, collides: ig.Entity.COLLIDES.FIXED, type: ig.Entity.TYPE.BOTH, _wmScalable: true, _wmDrawBox: true, _wmBoxColor: 'rgba(196, 255, 0, 0.1)', selectedImage: "undefined", sizeX: 96, sizeY: 80, imgReady: false, init: function( x, y, settings ) { var myImageString = "media/buildings/"+this.selectedImage+".png"; img = new ig.Image( myImageString); console.log(this.sizeX); img.width = img.data.width; img.height = img.data.height; img.load( this.imageLoaded.bind(this) ); this.parent( x, y, settings ); }, imageLoaded: function( path, success ) { if( success ) { this.imgReady = true; this.size.x = this.sizeX; this.size.y = this.sizeY; } else { ig.log( 'Error loading the image: ', path ); } }, draw: function() { if(this.imgReady == true) { img.draw(this.pos.x, this.pos.y); } } }); });