Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Is it possible to make the sprite rotation angle follow by joystick pad position??


khleug35
 Share

Recommended Posts

About the joystick plugin problem.

Is it possible to make the sprite rotation angle follow by joystick pad position??  

if the joystick pad move to right-left, the sprite angle is  right-left

like the following images

1.png.1e93db439c8c5d0f9867bfba007d2c35.png  2.png.73f6eb75615834235d9ca64a5679f27c.png 3.png.884a9270921dda10630fd62492a278fa.png

 

The following is my solution, but I think it is not the best way.

I create the hidden object and the joystick can control the hide sprite movement.

this.gun.rotation= this.gun.position.angle(game.scene.HiddenObject.point.position);  

like the following image

4.png.67cb0ab55cc53c883be77a1c36e2b059.png

 

 

Hidden object class

 game.createClass('HiddenObject', {
    init: function() { 
   
        this.point = new game.Sprite('circle2.png')
        this.point.anchorCenter();
        this.point.alpha =0
        
        
        this.body = new game.Body();
        this.body.collisionGroup = game.Body.POINTER;
        this.body.collideAgainst= [game.Body.WALL];
        this.body.position.x = 500;
        this.body.position.y = 600;
     
        var shape = new game.Rectangle(this.point.width, this.point.height);
        this.body.addShape(shape);
        this.body.addTo(game.scene.world);
        this.body.collide = this.collide.bind(this);
        var ths = this;
        
        
    },
    

    collide: function(body) {
      if (body.collisionGroup === game.Body.WALL) {
      
           return true;
        }
      
  
        return true;
    },
    
    update: function() {


	if(game.scene.joystick2.axis.x ==0 && game.scene.joystick2.axis.y ==0){
        this.body.velocity.x = 0;
        this.body.velocity.y = 0;
        }

	  this.point.position.x = this.body.position.x;
      this.point.position.y = this.body.position.y;
 
    this.body.velocity.x += game.scene.joystick2.axis.x * 4800;
    this.body.velocity.y += game.scene.joystick2.axis.y * 4800;
      
      
    } 

 

Rotation Sprite class

game.createClass('rotationsprite', {
    init: function() { 
    this.gun = new game.Sprite('gun.png');
    this.gun.anchorCenter();
    this.gun.position.set(450, 550);
    this.gun.addTo(game.scene.container);   
     

    },
    
    update: function(){         
        this.gun.rotation= this.gun.position.angle(game.scene.HiddenObject.point.position);  
        }
     
    }
    
    
});

 

Any idea,? thank, I am sorry about my poor English.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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