Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm having a hard time wrapping my head around crafty mechanics. Can I ask for a little explanation? #1193

Open
ogrotten opened this issue Sep 25, 2018 · 3 comments

Comments

@ogrotten
Copy link

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.

Is there a better place to post questions like this?

@starwed
Copy link
Member

starwed commented Sep 26, 2018

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.

@ogrotten
Copy link
Author

Thanks for responding

  1. Yeah i was simply trying to make the jsbin example and wasn't paying attention.
  2. 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?

@starwed
Copy link
Member

starwed commented Sep 30, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants