Phaser 3 从动态纹理创建 TileSprite

huangapple go评论48阅读模式
英文:

Phaser 3 create tilesprite from dynamic texture

问题

我已经在Phaser 3中开发了一个老虎机。轮轴本质上是在旋转过程中旋转的瓷砖精灵。

当轮轴减速时,我想加载一个包含代表支付引擎结果的符号的瓷砖精灵。由于我不能加载代表整个轮轴的瓷砖精灵(每个物理轮轴将包含超过100个图像),所以我已经开发的下面的代码需要修改,以便在加载"symbols"的地方加载包含预定图像的瓷砖精灵。

function spin_end(obj){
    // 将 obj 的瓷砖精灵停止在停止位置
    obj.y = get_stop('start', obj.id);
    obj.setTexture('symbols');
    // 应该改为 obj.setTexture('results'),我该如何创建 results???
    self.tweens.add({
        targets: obj,
        y: get_stop('end', obj.id),
        duration: 800,
        ease: 'Back.easeOut',
        onComplete(){
            if(obj.id === 4){
                calculate();
                is_spinning = false;
            }
        }
    });
    setTimeout(()=>{
        play_sound('Slot Machine Stop ' + Number(obj.id+1), self);
    }, 400);
}

希望这可以帮助你修改代码以加载包含预定图像的瓷砖精灵。

英文:

Phaser 3 从动态纹理创建 TileSpriteI have developed a slot machine in Phaser 3. The reels are essentially tile sprites that rotate during spinning.
When the reels slow down, I would like to load a tile sprite of symbols representing the outcome of the payout engine.
Since I can't load a tile sprite representing the entire reel (each physical reel would contain more than 100 images), the below code I have developed needs to be modified so that instead of loading "symbols", it loads a tile sprite containing predetermined images.

function spin_end(obj){
        //stop obj tilesprite at stop position
		obj.y = get_stop('start', obj.id);
		obj.setTexture('symbols');
        //it should be changed in  obj.setTexture('results') how can i create results???
		self.tweens.add({
			targets: obj,
			y: get_stop('end', obj.id),
			duration: 800,
			ease: 'Back.easeOut',
			onComplete(){
				if(obj.id === 4){
					calculate();
					is_spinning = false;
				}
			}
		});
		setTimeout(()=>{
			play_sound('Slot Machine Stop '+Number(obj.id+1), self);
		}, 400);
	}

答案1

得分: 0

I don't really understand your problem. You can generate reels with tilesprites and randomly set the result, like in the demo below. you don't need to have all combinations pre-created.

Mini Slot-machine - Demo:

document.body.style = 'margin:0;';

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    scene: { create },
    banner: false
};

function create () {
    this.add.text(10, 10, 'Click to start')
        .setScale(1.5)
        .setOrigin(0)
        .setStyle({ fontStyle: 'bold', fontFamily: 'Arial' });

    this.add.text(250, 10, 'TileSprite')
        .setScale(1.5)
        .setOrigin(0)
        .setStyle({ fontStyle: 'bold', fontFamily: 'Arial' });

    let graphics = this.make.graphics();
    createReelGraphics(graphics);

    this.add.image(380, 10, 'img').setOrigin(0).setScale(1.5);

    this.reel1 = this.add.tileSprite(110, 85, 10, 10, 'img').setScale(3);
    this.reel2 = this add.tileSprite(145, 85, 10, 10, 'img').setScale(3);
    this.reel3 = this.add.tileSprite(180, 85, 10, 10, 'img').setScale(3);

    startSpin(this);

    this.input.on('pointerdown', () => startSpin(this));
}

function startSpin(scene) {
    setRandomStartPosition(scene);
    scene.tweens.add({
        targets: [scene.reel1, scene.reel2, scene.reel3],
        tilePositionY: '+=100',
        duration: 800,
    });
}

function setRandomStartPosition(scene) {
    scene.reel1.tilePositionY = Phaser.Math.Between(0, 8) * 10;
    scene.reel2.tilePositionY = Phaser.Math.Between(0, 8) * 10;
    scene.reel3.tilePositionY = Phaser.Math.Between(0, 8) * 10;
}

new Phaser.Game(config);

function createReelGraphics(graphics) {
    let colors = [0xFF0000, 0xFF00DC, 0xB200FF, 0x4800FF, 0x0094FF, 0x00FFFF, 0x00FF90, 0x4CFF00, 0xFFD800, 0xFF6A00];

    for (let idx in colors) {
        graphics.fillStyle(colors[idx]);
        graphics.fillRect(0, (idx * 10), 10, 10);
    }

    graphics.generateTexture('img', 10, colors.length * 10);
}

I hope this demo helps a bit or at least helps clarify the problem.

英文:

I don't really understand your problem. You can generate reels with tilesprites and randomly set the result, like in the demo below. you don't need to have all combinations pre created.

Mini Slot-machine - Demo:
<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

document.body.style = &#39;margin:0;&#39;;
var config = {
type: Phaser.AUTO,
width: 536,
height: 183,
scene: { create },
banner: false
}; 
function create () {
this.add.text(10,10, &#39;Click to start&#39;)
.setScale(1.5)
.setOrigin(0)
.setStyle({fontStyle: &#39;bold&#39;, fontFamily: &#39;Arial&#39;});
this.add.text(250 ,10, &#39;TileSprite&#39;)
.setScale(1.5)
.setOrigin(0)
.setStyle({fontStyle: &#39;bold&#39;, fontFamily: &#39;Arial&#39;});
let graphics = this.make.graphics();
createReelGraphics(graphics);
this.add.image( 380, 10, &#39;img&#39; ).setOrigin(0).setScale(1.5);
this.reel1 = this.add.tileSprite(110, 85, 10, 10, &#39;img&#39;).setScale(3);
this.reel2 = this.add.tileSprite(145, 85, 10, 10, &#39;img&#39;).setScale(3);
this.reel3 = this.add.tileSprite(180, 85, 10, 10, &#39;img&#39;).setScale(3);
startSpin(this);
this.input.on(&#39;pointerdown&#39;, () =&gt; startSpin(this));
}
function startSpin(scene){
setRandomStartPosition(scene);
scene.tweens.add({
targets: [scene.reel1, scene.reel2, scene.reel3] ,
tilePositionY: &#39;+=100&#39;,
duration: 800,
});
}
function setRandomStartPosition(scene){
scene.reel1.tilePositionY = Phaser.Math.Between(0, 8) * 10
scene.reel2.tilePositionY = Phaser.Math.Between(0, 8) * 10
scene.reel3.tilePositionY = Phaser.Math.Between(0, 8) * 10
}
new Phaser.Game(config);
function createReelGraphics(graphics){
let colors = [ 0xFF0000, 0xFF00DC, 0xB200FF, 0x4800FF, 0x0094FF, 0x00FFFF, 0x00FF90, 0x4CFF00, 0xFFD800, 0xFF6A00];
for(let idx in colors){
graphics.fillStyle(colors[idx]);
graphics.fillRect(0, (idx * 10), 10, 10 );
}
graphics.generateTexture(&#39;img&#39;, 10, colors.length * 10);
}

<!-- language: lang-html -->

&lt;script src=&quot;//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

> I hope this demo helps abit, or atleast helps to clarify the problem.

Update - Dynamic Reels:
The code might need some improvement.
If you load the sprite with this.load.sprite(...) you don't need all the crazy workaround of the createReelGraphics function.

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-js -->

document.body.style = &#39;margin:0;&#39;;
var config = {
type: Phaser.AUTO,
width: 536,
height: 183,
scene: { create },
banner: false
}; 
new Phaser.Game(config);
console.clear();
function create () {
this.add.text(10,10, &#39;Reels - Click to rnd create&#39;)
.setScale(1.5)
.setOrigin(0)
.setStyle({fontStyle: &#39;bold&#39;, fontFamily: &#39;Arial&#39;});
let graphics = this.make.graphics();
createReelGraphics(this, graphics);
let serverData = rndData();
this.reels = []
createReels(this, serverData)
this.input.on(&#39;pointerdown&#39;, () =&gt; createReels(this, rndData()) );
}
// crazy code, just ignore :)
function rndData(){
return &#39;1&#39;.repeat(3).split(&#39;&#39;).map( x =&gt; &#39;1&#39;.repeat(4).split(&#39;&#39;).map( y =&gt; Phaser.Math.Between(0,9)) )
}
// create image-textures for the each reels
function createReels(scene, reelData){
for(let reel of scene.reels){
reel.destroy();
}
scene.reels = [];
for(let realIdx in reelData){
let reelKey = `reel${realIdx}`;
let reelHelper = scene.make.renderTexture(
{x: 0, y: 0, width: 10, height: 10 * reelData[realIdx].length, add: false}
);
for(let symbolIdx in reelData[realIdx]){
let helper = scene.make.image({ x: 0,
y: 10 * symbolIdx,
key: &#39;reel&#39;,
frame: reelData[realIdx][symbolIdx],
add: false })
.setOrigin(0);
reelHelper.draw(helper);
}
if(scene.textures.exists(reelKey)){
scene.textures.remove(reelKey);
}
reelHelper.saveTexture(reelKey);
scene.reels.push(scene.add.image( 40 + (40 * realIdx), 100, reelKey).setScale(2.5));
}
}
// If you load the image as a sprite you dont neet all this
function createReelGraphics(scene, graphics){
let colors = [ &#39;#FF0000&#39;, &#39;#FF00DC&#39;, &#39;#B200FF&#39;, &#39;#4800FF&#39;, &#39;#0094FF&#39;, &#39;#00FFFF&#39;, &#39;#00FF90&#39;, &#39;#4CFF00&#39;, &#39;#FFD800&#39;, &#39;#FF6A00&#39;];
let canvasSprite = scene.textures.createCanvas(&#39;reel&#39;, 10, 10 * colors.length);
let ctx = canvasSprite.context;
for(let idx in colors){
ctx.fillStyle = colors[idx];
ctx.fillRect(0, (idx * 10), 10, 10 );
canvasSprite.add(idx, 0, 0, idx * 10, 10, 10);
}
canvasSprite.refresh();
}

<!-- language: lang-html -->

&lt;script src=&quot;//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月9日 00:04:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433767.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定