Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by inihility

Hey folks, I'm trying to create an entity that I can move around on the game screen.

(I am not using the level editor)

So far I have only been able to render an entity through spawnEntity() if I used ig.Image and drew it in its own draw function. I would then spawn it wherever I wanted in an update function like so:

        update: function() {
            if (this.init) {
		ig.game.spawnEntity(EntityTitle, 0, 0);
		this.init = false;
	    }
	    this.parent();   
        },

The problem it seems I am encountering right now is that I am unable to transform the entity's position via. velocity (this.vel.x/y) probably because I am drawing the image at an already specified location.

Basically I'm trying to figure out if it is possible to move an entity that renders an image with ig.Image.

1 decade ago by inihility

A hack method I put together that kind of works with ig.Image but is far from elegant:

	update: function(){
		console.log(this.pos.y);
		if (this.pos.y < 150) {
			this.pos.y++;
		}
		else{
		}
		
		
		this.parent();
	},
	
        draw: function(){
                this.image.draw(x - this.image.width/2, y - this.image.height/2 - this.pos.y);;
        }

This does not seem like an ideal solution and causes some confusion about the actual position of the entity. Any insights would be great.

1 decade ago by lTyl

I'm not sure why you are complicating things. All you need to do is load up your image to draw and then in your entity, draw the image using the entity positions. Obviously, if you use a static value you won't be able to change it.

Your entity, that draws an image:
ig.module(
    'game.entities.entity'
)
    .requires(
    'impact.entity'
)
    .defines(function () {
        EntityImage = ig.Entity.extend({
            image:new ig.Image('media/image.png'),

            type:ig.Entity.TYPE.A,
            checkAgainst:ig.Entity.TYPE.B,
            collides:ig.Entity.COLLIDES.ACTIVE,

            init:function (x, y, settings) {
                this.parent(x, y, settings);
            },

            update:function () {
                this.parent();
                // Do update stuff
            },

            draw: function(){
                this.parent();

                // Draw the image
                this.image.draw(this.pos.x, this.pos.y);
            }
        })
    });

When you use ig.game.spawnEntity(EntityImage, x, y); the entity is spawned at the location and the image is drawn (From the entity draw code). Then when you manipulate the entity using vel.x/.y, your image will be re-drawn at the new entity coordinates.

1 decade ago by inihility

I see, I was really over complicating things - thanks a lot!
Page 1 of 1
« first « previous next › last »