Jump to content

Sine Wave movement on drag


logiclion
 Share

Recommended Posts

Hey guys!

 

First time posting here and I'm really new to using phaser.

So far I'm having a fun time learning how to use this framework, but I'm having a bit of trouble with moving my sprite.

 

Basically I have a sprite drawn to the left of the game canvas, around 0,300 (On a 800, 600 canvas) and I want to be able to drag my sprite and it moves in a sine wave across the screen. I've been playing and getting the movement right of the sine wave, but when I click to drag the sprite, it moves it up on the screen. Any tips on making the box stay fixed when I first click it to drag?

 

Thank you very much!

 

Right now I have this in my code:

function create() {
 
    //  This creates a simple sprite that is using our loaded image and
    //  displays it on-screen
    block = game.add.sprite(0, 0, 'block');
    block.inputEnabled = true;
    block.input.enableDrag();
    game.stage.backgroundColor =  '#ffffaa';
    block.input.allowVerticalDrag = false;
    block.y = game.height/2-(block.height);
    block.x = block.width/2;
    orginX = block.x;
    orginY = block.y;
    block.events.onDragStop.add(onDragStop, this);
}
 
function onDragStop(sprite, pointer) {
    block.x = orginX;
    block.y = orginY;
}
 
function update(){
    console.log(block.x);
    if(block.input.isDragged){
        block.y = (100*Math.sin(block.x/35));
    }
}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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