Attach my sprite with Box2d

Posted by user919496 on Game Development See other posts from Game Development or by user919496
Published on 2012-10-21T12:27:39Z Indexed on 2012/10/21 17:20 UTC
Read the original article Hit count: 167

Filed under:
|

I'm coding Javascript(HTML5) with Box2D. And I want to ask how to attach Sprite with Box2D.

This is function My sprite:

function My_Sprite() {
    this.m_Image = new Image();
    this.m_Position = new Vector2D(0,0);
    this.m_CurFrame = 0;
    this.m_ColFrame = 0;
    this.m_Size = new Vector2D(0,0);
    this.m_Scale = new Vector2D(0,0);
    this.m_Rotation = 0;
}
My_Sprite.prototype.constructor = function (_Image_SRC) {

    this.m_Image.src = _Image_SRC;
}
My_Sprite.prototype.constructor = function (_Image_SRC,_Size,_Col) {

    this.m_Image.src = _Image_SRC;
    this.m_Size = _Size;
    this.m_ColFrame = _Col;
    this.m_Scale = new Vector2D(1, 1);
}
My_Sprite.prototype.Draw = function (context) {
    context.drawImage(this.m_Image,
        this.m_Size.X * (this.m_CurFrame % this.m_ColFrame),
        this.m_Size.Y * parseInt(this.m_CurFrame / this.m_ColFrame),
        this.m_Size.X, this.m_Size.Y,
        this.m_Position.X, this.m_Position.Y,
        this.m_Size.X * this.m_Scale.X, this.m_Size.Y * this.m_Scale.Y
        );
}

and this is function Object :

function Circle(type, angle, size) {

  //  Circle.prototype = new My_Object();
   // Circle.prototype.constructor = Circle;
   // Circle.prototype.parent = My_Object.prototype;

    this.m_den = 1.0;
    this.m_fri = 0.5;
    this.m_res = 0.2;

    fixDef.density = this.m_den;
    fixDef.friction = this.m_fri;
    fixDef.restitution = this.m_res;
    fixDef.shape = new b2PolygonShape;

    bodyDef.type = type;
    bodyDef.angle = angle;
    bodyDef.userData = m_spriteCircle;

    fixDef.shape = new b2CircleShape(
           Radius / SCALE //radius
        );
    this.m_Body = world.CreateBody(bodyDef);
    this.m_Body.CreateFixture(fixDef);

    m_spriteCircle = new My_Sprite();

    this.Init();
}
Circle.prototype.Init = function () {

    m_spriteCircle.constructor("images/circle.png", new Vector2D(80, 80), 1);
    m_spriteCircle.m_CurFrame = 0;



}


Circle.prototype.Draw = function (context) {
    m_spriteCircle.Draw(context);
}

and I draw it :

   var m_Circle = new Circle();

   m_Circle.Draw(context);

© Game Development or respective owner

Related posts about JavaScript

Related posts about box2d