简体   繁体   English

"在移相器 3 中绘制移动线"

[英]Draw moving line in phaser 3

I want to draw line, where it's first pair of coordinates situated in the center of drew circle and it's second pair will be unsettled until I direct it to the cirlce with the same color, which have first one.我想画线,它的第一对坐标位于画圆的中心,第二对坐标将不稳定,直到我将它指向具有相同颜色的圆圈,其中有第一个。 Do you have any ideas?你有什么想法? 在此处输入图像描述

My code:我的代码:

let config = {
            type: Phaser.AUTO,
            width: 800,
            height: 600,
            backgroundColor: '#f0ebeb',
            physics: {
                default: 'arcade',
                arcade: {
                    gravity: { y: 300 },
                    debug: false
                }
            },
            scene: {
                preload: preload,
                create: create,
                update: update
            },

            scale: {
                autoCenter: Phaser.Scale.CENTER_BOTH
            }
        };

        let game = new Phaser.Game(config);
        let items = [];

        let dots =  new Map([
            [1, '#4293f5'],
            [2, '#42f554'],
            [3, '#f5e942'],
            [4, '#f55a42'],
            [5, '#f542c8'],
        ])

        function preload() {
            
        }
        function create() {
            let x = 100;
            let y = 0;

            for (i = 0; i < 36; i++) {
                if (i % 6 === 0) {
                    y += 85;
                    x = 100;
                }
                this.add.circle(x, y, 35, parseInt(dots.get(getRandomInt(5)).replace(/^#/, ''), 16));
                x += 125;
            }
            
        }
        function update() { }

        function getRandomInt(max) {
            return Math.floor(Math.random() * max) + 1;
        }

What I am trying to do: https://play.google.com/store/apps/details?id=com.nerdyoctopus.gamedots我想要做什么: https ://play.google.com/store/apps/details?id=com.nerdyoctopus.gamedots

A quick possible solution could be to use the pointer Events ( pointerdown , pointermove and pointerup ) of the scene.一种可能的快速解决方案是使用场景的指针事件( pointerdownpointermovepointerup )。 Here is a demo code, with the basic functionality这是一个演示代码,具有基本功能

 let config = { type: Phaser.AUTO, parent: 'phaser-example', width: 400, height: 200, scene: { create } }; let game = new Phaser.Game(config); let isDragging = false; let lineStartPosition = {x:0 , y:0}; let line; function create () { let cicles = [] for(let rowIdx = 0; rowIdx < 4; rowIdx++ ){ for(let colIdx = 0; colIdx < 2; colIdx++ ){ let circle = this.add.circle(50 + 100 * rowIdx, 50 + 100 * colIdx, 25, 0x6666ff).setOrigin(.5); circle.setInteractive(); cicles.push(circle); } } line = this.add.line(0,0, 0,0, 100, 100, 0xffffff).setOrigin(0); line.setLineWidth(5); line.visible = false; // adding the events to the scene this.input.on('pointerdown', dragStart); this.input.on('pointerup', dragEnd); this.input.on('pointermove', drag); } function dragStart(pointer, gameObjects){ if(gameObjects.length == 0) return lineStartPosition.x = gameObjects[0].x; lineStartPosition.y = gameObjects[0].y; isDragging = true; line.x = gameObjects[0].x; line.y = gameObjects[0].y; line.setTo(0, 0, 0, 0); line.visible = true; } function drag(pointer, gameObject){ if(isDragging == true){ line.setTo(0, 0, pointer.x - lineStartPosition.x, pointer.y - lineStartPosition.y); } } function dragEnd(pointer, gameObject){ isDragging = false; }
 <script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

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

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