Jump to content

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


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




Hidden object class

 game.createClass('HiddenObject', {
    init: function() { 
        this.point = new game.Sprite('circle2.png')
        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.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.position.set(450, 550);

    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

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.

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.


  • Recently Browsing   0 members

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