Jump to content

Drag sprite on android


kornik
 Share

Recommended Posts

var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

function preload() {

  game.load.image('sky', 'assets/sky.png');
  game.load.image('ground', 'assets/platform.png');
  game.load.image('wall', 'assets/wall.png');
  game.load.image('star', 'assets/star.png');
  game.load.image('kit', 'assets/firstaid.png');
  game.load.spritesheet('dude', 'assets/dude.png', 32, 48);
  game.load.spritesheet('bad', 'assets/baddie.png', 32, 48);

}

var star;
var platforms;
var cursors;
var ground;
var rightBorder;
var leftBorder;
var kits;
var vel = 300;

function create() {

  game.physics.startSystem(Phaser.Physics.ARCADE);

  star = game.add.sprite(335, 25, 'star');
  star.inputEnabled = true;
  star.input.enableDrag(true);
  game.physics.arcade.enable(star);
  star.body.moves = false

  platforms = game.add.group();
  platforms.enableBody = true;

  ground = platforms.create(19, game.world.height - 100, 'ground');
  ground.body.velocity.x = vel;
  ground2 = platforms.create(200, 350,  'ground');
  ground2.body.velocity.x = -vel;
  ground3 = platforms.create(19, 100, 'ground');
  ground3.body.velocity.x = vel;
  ground4 = platforms.create(300, 200, 'ground');
  ground4.body.velocity.x = -vel;
  leftBorder = platforms.create(-50, game.world.height - 600, 'wall');
  rightBorder = platforms.create(785, game.world.height - 600, 'wall');
  leftBorder.scale.setTo(2, 2);
  rightBorder.scale.setTo(2, 2);

  kits = game.add.group()
  kits.enableBody = true;
  kit = kits.create(350, 150, 'kit')
  kit2 = kits.create(350, 270, 'kit')
  kit3 = kits.create(350, 430, 'kit')
  kit4 = kits.create(350, 550, 'kit')

}

function update() {
  game.physics.arcade.overlap(star, platforms, youLose);
  checkWall(ground);
  checkWall(ground2);
  checkWall(ground3);
  checkWall(ground4);
  game.physics.arcade.overlap(star, kits, checkAid, null, this);
  if (kits.length < 1) {
    star.destroy()
  }
}

function youLose() {
  star.destroy();
}

function checkWall(platform) {
  if (game.physics.arcade.overlap(platform, rightBorder)) {
    platform.body.velocity.x = -vel
  } else if (game.physics.arcade.overlap(platform, leftBorder)) {
    platform.body.velocity.x = vel
  }
}

function checkAid(star, kit) {
  kit.destroy();
}

Hi there ! First time writing here, and also very new to Phaser and game development in general. I have a problem with dragging a sprite on android device after compiling using Cocoon JS. Everything works fine in the browser, but for some reason I can't move the sprite (star) after installing game on my phone.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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