I'm having a hard time wrapping my head around crafty mechanics. Can I ask for a little explanation?
ogrotten opened this issue · 3 comments
I posted this to the google group, then saw that the group hasn't had activity in "a while" so I pulled it to post here.
To get my first steps with crafty, I figure I'd build a solitaire game. I have a card face spritesheet, and used the JS object method of defining the cards. As a baby step, I just want to show one card (the position is defined as "AceHearts") before I delve into any real logic.
On the Sprites intro http://craftyjs.com/documentation/sprites.html, I KINDA get how the animation is actually called. I see in the go()
function that a variable is declared, but it's never used, so that's weird to my brain. But I get that the chained functions, .reel
and .animate
, are what's drawing the sprite anim to the screen.
Based on the "Static Sprite" section of the above linked page, I don't understand how the single sprite is being called. In the JSBin space there, when I change var small_sprite
from the animated sprite to the static sprite, it shows the single frame.
However, when I do this with my own asset, I get nothing on screen and no errors in console. According to the chrome inspector, all my assets are loading, and there's no errors, but I get nothing.
- Here's the really simple code I've got: http://jsbin.com/vitumav/1/edit?html,js,output
- The locally linked version of crafty is from last week, 0.9.0
- I wasn't sure if CORS would be a problem so I left in the call to the local playing card spritesheet, but the original is here: http://showcase.codethislab.com/games/joker_poker/readme/assets/images/card_spritesheet.png
Is there a better place to post questions like this?
Is there a better place to post questions like this?
Here is fine for now. :)
I think there are a few issues with how you've set up the jsbin:
- You've included HTML in the javascript tab
- You probably need to link to Crafty via the full http url (e.g.
<script src="https://cdn.rawgit.com/craftyjs/Crafty/testing/dist/crafty-min.js"></script>
)
You also define the tile
property of the sprite map-- I'm guessing one of those should be tileh
?
When I tweaked these, I could see the sprite rendering ok.
I see in the go() function that a variable is declared, but it's never used, so that's weird to my brain.
Yup, that's just to make it easy to follow the suggestion about adjusting run speed.
Thanks for responding
- Yeah i was simply trying to make the jsbin example and wasn't paying attention.
- The fix was
tileh
I added one character to the source andfixed it. My favorite kind of bug.
So to see that I have it straight:
when using Crafty.e
(like in my go()
function), is using it as a variable optional? What are the caveats of not doing so?
when using Crafty.e (like in my go() function), is using it as a variable optional? What are the caveats of not doing so?
It's always optional -- once you've created an entity, Crafty will keep a reference to it internally. For many uses, you can find it later by using the various core Crafty selectors. (e.g. Crafty("AceHearts")
will return a list of all entities with the AceHarts component attached. See selecting components for more details.)
The main reason to assign to a variable is if you want to keep a reference to that specific entity. The alternative is generally to use the events system instead.
For instance, imagine you had a projectile that was supposed to home in on an enemy. There are two fairly different ways you could solve this:
- The projectile stores a direct reference to the target enemy, and every frame looks at the current position of the enemy and changes it's trajectory accordingly.
- The projectile binds to the 'Move' event of the entity, and when that fires changes trajectory.
I'm not sure one is really better than the other. Using direct references can be a bit simpler conceptually, but binding to events can be more flexible.