2017-03-27 27 views
0

我有一個使用Lime/Corona JSON物理文件(從PhysicsEditor導出)的圓形精靈(彈球)。這個文件的目的是讓hitbox實際上是循環的。調整具有自定義物理文件的精靈的大小? (P2/Phaser)

它的正常工作(對於那些不熟悉的CoffeeScript中,@只是意味着this

# in preload 
@load.physics 'ball_physics', @Assets.ball_physics 

# in create 
@ball = @add_p2_sprite @ball_start_x, @ball_start_y, 'ball' 
@ball.body.clearShapes() 
@ball.body.loadPolygon 'ball_physics', 'ball' 

的問題是,我不能縮放或調整大小球不會弄亂擊中格。它似乎總是以原始尺寸工作。有什麼辦法可以動態更新嗎?

例如,下面是一個使用非大小精靈:

enter image description here

這裏是與縮放到0.5精靈,0.5:

enter image description here

有什麼辦法除了爲sprite創建新的png和JSON文件之外,這麼做?


下面是評論的迴應,打開debugMode所以我看到hitboxes概述和應用物理文件之前縮放球:

@ball = @add.sprite @ball_start_x, @ball_start_y, 'ball' 
@ball.scale.x = 2 
@ball.scale.y = 2 
@physics.p2.enable @ball, true 
@add_physics_file(@ball, 'ball_physics', 'ball') 
@collide_world_bounds(@ball) 

的擊中格並沒有重新縮放:

enter image description here

+0

你有沒有擴展到多邊形應用物理學過嗎? –

+0

謝謝@Julián,這是一個很好的建議嘗試。不幸的是它沒有奏效 - 我用這個嘗試和結果更新了這個問題。 –

回答

1

我發現一個megmut用戶代碼,我試過了,它工作完美,你可以試試看:

function resizePolygon(originalPhysicsKey, newPhysicsKey, shapeKey, scale){ 
    var newData = []; 
    var data = this.game.cache.getPhysicsData(originalPhysicsKey, shapeKey); 

    for (var i = 0; i < data.length; i++) { 
     var vertices = []; 

     for (var j = 0; j < data[i].shape.length; j += 2) { 
      vertices[j] = data[i].shape[j] * scale; 
      vertices[j+1] = data[i].shape[j+1] * scale; 
     } 

     newData.push({shape : vertices}); 
    } 

    var item = {}; 
    item[shapeKey] = newData; 
    game.load.physics(newPhysicsKey, '', item); 

} 

只需使用:

function preload() { 
     game.load.image('contra2', 'contra2.png'); 

     game.load.physics('physicsData', 'sprites.json'); 
    } 

    function create() { 
     game.physics.startSystem(Phaser.Physics.P2JS); 

     var scale = 1.5; 
     resizePolygon('physicsData', 'robot', 'contra2', scale) 

     contra = game.add.sprite(400, 300, 'contra2'); 
     contra.scale.setTo(scale, scale); 

     game.physics.p2.enable(contra, true); 

     contra.body.clearShapes(); 
     contra.body.loadPolygon('robot', 'contra2'); 
    } 
+0

太棒了!當我把它放在'create'中時,效果很好,但不是在'preload'中。 –

+0

終於一場公平的彈球遊戲https://media.giphy.com/media/3og0IGFCjEmsLkemrK/giphy.gif –