Jump to content

New to Pixijs... can I just add attributes to a Sprite?


kinome79
 Share

Recommended Posts

So, learning Pixijs by building a game... created my ship, and it flies around with an asteroid like dynamic, leaving me to track its x and y velocities, if its accelerating, turning, firing, etc. I usually build a class for this, but the following seems to work for my purpose.

let ship = PIXI.Sprite.from('images/sprite.png');
ship.width=50;
ship.height=20;
ship.speed = .2;
ship.rotspeed = .07;
ship.xvel = 0;
ship.yvel = 0;
ship.rotating = 0;
ship.accelling = false;
ship.braking = false;
ship.shooting = false;

This seems to work just fine, and my ship is flying around without issue. Tried searching if this is OK, I mainly find implementations of set/getAttr, creating classes, etc. I haven't tried the same thing with a method yet ( ship.explode = () => {} ) but can almost imagine that will work too. Thoughs? Is this going to lead to problems down the road? 

Link to comment
Share on other sites

  • 2 weeks later...

Hi, @kinome79! Your code is perfectly fine for a beginner. You can do like this while you learn. Also it will be ok to do so when you're just prototyping a new project and not taking it serious. But after some time, when you'll want to progress in development or make make big complex projects - it will become a little bit hard to maintain such code style. For big projects you'll probably need more of OOP, classes and stuff. Hope this helps!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...