Jump to content

Click the stage and have a sprite move to the clicked area


forleafe
 Share

Recommended Posts

I feel embarrassed, but after doing a lot of research I can't seem to be able to implement this simple feature. Can anyone here who's familiar with pixi walk me through how I might accomplish this?

Things I've tried... I'm trying to use  .on('click', move)

and then I have the move function right now set to just return the value of the pointer. I can't even get my code to return anything, let alone move the sprite.

I've tried doing stage.on('click', move); and it just doesn't work. I get no errors in the console. nothing. It just doesn't work. After that, I tried creating a rectangle and using rectangle.on('click', move); I even fill in the rectangle with a color to make sure it's actually there. Nothing. my code is just unresponsive. No errors no nothing.

The only time my code works is when I use the .on('click', move) on the actual sprite I'm trying to move! sprite.on('click', move). It will then return 'undefined' indicating that I at least got that far...

I can provide my code if needed. But first I wanted to just check in and see what the best practice is for this, and if I'm just missing something elementary.

 

Link to comment
Share on other sites

29 minutes ago, babycarlitos said:

I have no idea why I thought that you wanted drag. I just made this check it out to see if it is what you need

 

https://jsfiddle.net/babycarlitos/9nLLc7je/32/ 

Hi babycarlitos, 

I was also looking for a similar function, I want to make my pixi stage interactive, so it will respond to click events such as right click and similar. I'm just looking at your code to try and see how you've done it. From what I can see, you've created an invisible background component/element/sprite which responds to the click events, and then moves the red box to the point where the mouse was clicked.

Am correct to say this is what you've done?

Link to comment
Share on other sites

Hello cnwerb,

Yes that is pretty much what I did. I made a blackish sprite that is the size of the window and added events to it. You can take the code and expand on it to add more events. In my case my "stage" is a container and making the stage interactive has a different behavior. Adding a sprite seems much easier.

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