Jump to content

input doesn't work when using camera


embatbai
 Share

Recommended Posts

title says it all. I have a camera focused on the character and this function:
state.clicked = function(){
this.isClicked = true;
this.heart.x = this.game.input.x;
}

The heart doesn’t line up with where I clicked and changes in relation to the character when she moves.

Link to comment
Share on other sites

This sounds very similar to an issue that was raised elsewhere recently, so we have some good answers.

 

The camera is actually a transformation matrix applied to the entire scene. You can rotate and scale it. You can scale it a different amount on different axes and then rotate it, causing shear. While you might never want to do this, it will make simple input mapping solutions break down and cry.

 

Luckily, there are solutions.

 

Camera.transformPoint( Kiwi.Geom.Point ) will take a point in screen space (such as input values) and tell you where that is in world space.

 

There’s also an inverse, Camera.transformPointToScreen( point ), which may be useful for assessing whether an object is on screen or not.

 

Finally, input components are available on Sprites, which send a Signal on input (sprite.input.onRelease.add(), etc). These do respond correctly to camera transformations, even the extreme sort with scaling etc. You can get a fair amount of data out of this. Basically, you pass a function to the Signal, and it’s called when that signal is triggered by the input system. A bare anonymous function with no parameters works fine, e.g. function() { Kiwi.Log.log( "#debug", "Input received" ); }.

 

However, the input system will pass it a couple of optional parameters, which the function can use: a Sprite and a Cursor (just the sprite and cursor that are interacting, really). You can see these working in the console by adding parameters to the function passed to the signal: function( sprite, cursor ) { Kiwi.Log.log( "#debug", "Input received", sprite, cursor ); }. In particular, the cursor comes with a lot of data about its position on the screen, what modifiers are in use, how long it’s been down etc. Note that this information does not take any camera transformations into account, so it will still have to be transformed by the above methods; however, the camera is consulted when determining whether the cursor and the sprite interacted at all.

 

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...