简体   繁体   English

Phaser 3:绘制实时曲线

[英]Phaser 3: Draw a live curve

I am following the post:我正在关注这个帖子:

https://cedarcantab.wixsite.com/website-1/post/phaser-coding-tips1-2-revisited-part-1-creating-a-game-like-tanks---worms https://cedarcantab.wixsite.com/website-1/post/phaser-coding-tips1-2-revisited-part-1-creating-a-game-like-tanks---worms

And I am trying to draw a live curve that determines where the bullet overlaps the land before shooting.我正在尝试绘制一条实时曲线,以确定子弹在射击前与土地重叠的位置。 Does anyone have any ideas for this situation?有没有人对这种情况有任何想法? Thank you.谢谢你。

A solution without much calculations could be, use a dummy object as a "tracer bullet" .没有太多计算的解决方案可能是使用虚拟对象作为“示踪子弹”
With other Words, make a physics object, that doesn't collide with anything, "shoot" that object, and draw, in a specific interval a small "marker" , that would traces the arc / path taken.换句话说,制作一个不与任何东西碰撞的物理对象, “射击”该对象,并在特定间隔内绘制一个小的“标记” ,以跟踪所采用的弧/路径。

Here is a small demo, showcasing this idea:这是一个小演示,展示了这个想法:

 var config = { type: Phaser.AUTO, width: 536, height: 183, scene: { create, update }, physics:{ default:'arcade', arcade:{ gravity:{y: 260}, }}, banner: false }; let player; let firstMarker; let pathItems; let lastMarker; function create () { this.add.text(10, 10, 'move mouse for targeting\nhold still to see the full path.', {color:'#ffffff'}) let floor = this.add.rectangle(0 , 183, 535, 20, 0x00ff00).setOrigin(0, .5); pathItems = this.add.group(); player = this.add.rectangle(30 , 158, 30, 30, 0x1AA840); player.setDepth(2) firstMarker = this.add.arc(-10, 0, 5, 0, 2 * Math.PI, true, 0xffffff); this.physics.add.existing(firstMarker); this.input.on('pointermove', function (pointer) { startMarkerTrail(this, player, pointer) },this); } function update(time, delta){ let {x, y} = firstMarker if(!lastMarker || Phaser.Math.Distance.Between(x, y, lastMarker.x, lastMarker.y ) > 20){ let marker = this.add.arc(x, y, 3, 0, 2 * Math.PI, true, 0xcdcdcd); pathItems.add(marker); lastMarker = {x, y}; } } function startMarkerTrail(sccene, player, pointer){ let speed = 300; let angle = Phaser.Math.Angle.BetweenPoints(player, pointer); pathItems.clear(true); firstMarker.setPosition( player.x, player.y ); sccene.physics.velocityFromRotation(angle, speed, firstMarker.body.velocity); } new Phaser.Game(config);
 * {padding:0;margin:0;}
 <script src="//cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

If you need a line can make the distance smaller and/or make the size of the markerpoints smaller.如果您需要一条线,可以使距离更小和/或使标记点的大小更小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM