parwaniprakash Posted June 26, 2014 Share Posted June 26, 2014 Hello everyone, I am creating a game in that one object (say bus or train) is running on the path. On the end of the path passenger need to add on that object and travel according to it. My question is how to add the object with a running object with any distraction. here is my code :- <!doctype html> <html> <head> <title>Phaser</title> <script type="text/javascript" src="libs/js/phaser.js"></script> </head> <body> <script type="text/javascript">var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload,create: create, update:update });var timeCheck = 0;var path = [[new Phaser.Point(100, 500),new Phaser.Point(500, 500)]];var pathSpriteIndex = [0];var sp = sprite = null;var pathIndex = linepath = pathSpIndex = 0;var flag = false; function preload(){ game.load.image('star', 'img/train1.png'); game.load.image('rect', 'img/rectangle.png');} function create() { game.stage.backgroundColor = '#454645'; graphic = game.add.graphics(0, 0); enemies = game.add.group(); enemies.enableBody = true; enemies.physicsBodyType = Phaser.Physics.ARCADE; sprite = [enemies.create(360 + Math.random() * 200, 120 + Math.random() * 200, 'star')]; shap = game.add.group(); shap.enableBody = true; shap.physicsBodyType = Phaser.Physics.ARCADE; shapes = [enemies.create(530,490, 'rect')]; game.physics.startSystem(Phaser.Physics.ARCADE); game.physics.enable(enemies, Phaser.Physics.ARCADE); game.physics.enable(shap, Phaser.Physics.ARCADE); graphic.lineStyle(10, '0xff0000', 1); graphic.moveTo(100, 500); graphic.lineTo(500, 500);} function update(){ for(i=0;i<path.length;i++) { arr1 = path; pathIndex = arr1.length; if (arr1 != null && arr1.length > 0 && pathSpriteIndex < pathIndex) { pathSpriteIndex = Math.min(pathSpriteIndex, path.length - 1); sp = sprite; sp.anchor.setTo(0.5, 0.5); sp.rotation = game.physics.arcade.moveToXY(sp, path[pathSpriteIndex].x, path[pathSpriteIndex].y, 100); sp1 = shapes; sp1.anchor.setTo(0, 0); sp1.rotation = game.physics.arcade.moveToXY(sp1, path[pathSpriteIndex].x, path[pathSpriteIndex].y, 100); if (game.physics.arcade.distanceToXY(sp, path[pathSpriteIndex].x, path[pathSpriteIndex].y) < 20) { pathSpIndex = pathSpriteIndex; pathSpIndex++; pathSpriteIndex = pathSpIndex; } if(pathSpriteIndex==pathIndex && pathIndex >1) { pathSpIndex = 0; pathSpriteIndex = pathSpIndex; } } } } </script> </body></html> Thanks in advance Link to comment Share on other sites More sharing options...
Recommended Posts