2016-11-28 5 views
1

フェイザードラッグ可能なグループ化された世界地図:http://www.emanueleferonato.com/2016/01/18/how-to-create-a-html-draggable-and-scrollable-map-with-inertia-using-phaser-framework/私は慣性でドラッグ可能なマップを作成するには、この偉大なチュートリアルを見つけた

チュートリアルだけで画像に適用されるドラッグ効果があります。代わりにスプライトのグループにドラッグエフェクトを適用しようとしているので、それらはすべて同時にドラッグします(マップ画像+スプライトのグループ)。

問題は、this.scrollingMapが構文上何を表しているのか少し混乱していることです。だから、この行をグループに置き換えることになると、私は少し失われてしまいます。

this.scrollingMap = game.add.image(0, 0, "map"); 

誰でもアイデアはありますか?

このような場合は、以下の簡易コードもコピーしました。

var game = new Phaser.Game(window.innerWidth, window.innerHeight, Phaser.AUTO, '', { preload: preload, create: create, update: update }); 
 

 

 
function preload() { 
 
    
 
    game.load.image('map', 'assets/images/baseMap.png'); 
 
    game.load.image('star', 'assets/images/star.png'); 
 
} 
 

 

 
function create() { 
 
    
 
    // Creating the group 
 
    world = game.add.group(); 
 
    world.create(0, 0, 'map'); 
 
    // Adding random sprites to it 
 
    for (var i = 0; i < 10; i++) 
 
    { world.create(game.world.randomX, game.world.randomY, 'star');} 
 
    
 
    //This group works on its own. 
 
    //I would like to link it to the dragging animation below "scrollingMap". 
 

 

 
    //The Draggable Map from the tutorial 
 
    // Adding the big map to scroll 
 
    this.scrollingMap = game.add.image(0, 0, "map"); //<-- This is where I am having trouble changing from an image to a group. 
 
    // map will accept inputs 
 
    this.scrollingMap.inputEnabled = true; 
 
    // map can be dragged 
 
    this.scrollingMap.input.enableDrag(false); 
 
    // custom property: we save map position 
 
    this.scrollingMap.savedPosition = new Phaser.Point(this.scrollingMap.x, this.scrollingMap.y); 
 
    // custom property: the map is not being dragged at the moment 
 
    this.scrollingMap.isBeingDragged = false; 
 
    // custom property: map is not moving (or is moving at no speed) 
 
    this.scrollingMap.movingSpeed = 0; 
 
    // map can be dragged only if it entirely remains into this rectangle 
 
    this.scrollingMap.input.boundsRect = new Phaser.Rectangle(game.width - this.scrollingMap.width, game.height - this.scrollingMap.height, this.scrollingMap.width * 2 - game.width, this.scrollingMap.height * 2 - game.height); 
 
    // when the player starts dragging... 
 
    this.scrollingMap.events.onDragStart.add(function(){ 
 
     this.scrollingMap.isBeingDragged = true; 
 
     // set movingSpeed property to zero. This will stop moving the map 
 
     // if the player wants to drag when it's already moving 
 
     this.scrollingMap.movingSpeed = 0; 
 
    }, this); 
 
    // when the player stops dragging... 
 
    this.scrollingMap.events.onDragStop.add(function(){ 
 
     this.scrollingMap.isBeingDragged = false; 
 
    }, this); 
 
    
 
} //End create function 
 

 

 
function update() { 
 
    
 
    // if the map is being dragged... 
 
    if(this.scrollingMap.isBeingDragged){ 
 
     this.scrollingMap.savedPosition = new Phaser.Point(this.scrollingMap.x, this.scrollingMap.y); 
 
    } 
 
    // if the map is NOT being dragged... 
 
    else{ 
 
     // if the moving speed is greater than 1... 
 
     if(this.scrollingMap.movingSpeed > 1){ 
 
       this.scrollingMap.x += this.scrollingMap.movingSpeed * Math.cos(this.scrollingMap.movingangle); 
 
       this.scrollingMap.y += this.scrollingMap.movingSpeed * Math.sin(this.scrollingMap.movingangle); 
 
       if(this.scrollingMap.x < game.width - this.scrollingMap.width){ 
 
        this.scrollingMap.x = game.width - this.scrollingMap.width; 
 
       } 
 
       if(this.scrollingMap.x > 0){ 
 
        this.scrollingMap.x = 0; 
 
       } 
 
       if(this.scrollingMap.y < game.height - this.scrollingMap.height){ 
 
        this.scrollingMap.y = game.height - this.scrollingMap.height; 
 
       } 
 
       if(this.scrollingMap.y > 0){ 
 
        this.scrollingMap.y = 0; 
 
       } 
 
       this.scrollingMap.movingSpeed *= friction; 
 
       // save current map position 
 
       this.scrollingMap.savedPosition = new Phaser.Point(this.scrollingMap.x, this.scrollingMap.y); 
 
     } 
 
     // if the moving speed is less than 1... 
 
     else{ 
 
       var distance = this.scrollingMap.savedPosition.distance(this.scrollingMap.position); 
 
       var angle = this.scrollingMap.savedPosition.angle(this.scrollingMap.position); 
 
       if(distance > 4){ 
 
        this.scrollingMap.movingSpeed = distance * speedMult; 
 
        this.scrollingMap.movingangle = angle; 
 
       } 
 
     } 
 
    } 
 
}

答えて

2

だから、私は解決策を見つけてしまった...オフ

まず、私はすべてのこの .scrollingMapを削除し、削除するためにscrollingMapにそれらを変更しました混乱。完璧に働くスチールを終わらせました。

scrollingMap = game.add.image(0, 0, "map"); 
scrollingMap.anchor.set(0.05,0.5); 
scrollingMap.inputEnabled = true; 
[etc...] 

次に、フェイザーでグループは一度だけで、一緒要素に取り組んで入力を持つことができるようには見えません。だから、動作しないでしょうのようなものに変更:

scrollingMap = game.add.group(); 
map = game.add.image(0, 0, "map"); 
scrollingMap.add(map); 
// The following line won't work 
scrollingMap.inputEnabled = true; 

私はフェイザーを提供しています合わせ機能を使用してみました...私はあなたがそうのような他のスプライト内で実際に巣のスプライトをできることを実現することになったまで。

scrollingMap = game.add.image(0, 0, "map"); 
someSprite = game.add.image(100, 100, "sprite"); 
scrollingMap.addChild(someSprite); 

そしてvoila!それだけの解決策があります。あなたはまた、子供のようにグループを追加することができます

注: http://www.html5gamedevs.com/topic/7745-move-a-group-of-sprites-together-as-one-body/

:誰でも好奇心旺盛だ場合は、ここに解決策が見つかり

someGroup = game.add.group(); 
scrollingMap.addChild(someGroup);