I want to spawn a small-vfx sprite by coding, and I have tried to code something to realize the target, but the small-vfx sprite doesn’t appear during the game. I don’t know why and how to use sprite in coding correctly.
Here is the code I tried to realize the target (It is under the ‘pawn.js’, I want to spawn sprite when the player is moving):
var small_vfx = new pc.Entity();
var sprite01 = this.app.assets.find('small-vfx-01','sprite');
var sprite02 = this.app.assets.find('small-vfx-02','sprite');
var sprite03 = this.app.assets.find('small-vfx-03','sprite');
// var sprite_material = this.entity.sprite._material.clone();
small_vfx.setLocalPosition(pos);
small_vfx.addComponent("script");
small_vfx.script.create('smallVfx', {});
small_vfx.addComponent("sprite", {
type:"animated",
clips: {sprite01,sprite02,sprite03},
autoPlayClip: 'small-vfx-01',
spriteAsset: sprite01.id,
});
small_vfx.sprite._colorUniform = new Float32Array(([1, 1, 1]));
And here is the link of my project. Can someone help me?
Not sure what’s wrong here, I think the way you set the clips may be the problem. Normally I would use the addClip() method on the component, since it requires a number of properties to be passed per clip.
Why are you creating a new entity each time you move your pawn? This will have a serious performance issue eventually.
Instead of fully creating the sprite in code, why not have a disabled “template” entity and clone that instead? This way you can easily customize/tweak it in editor as well.
You may be able to get away with it if you a very simple game with a small number of objects. But as a rule of thumb when you are doing live rendering, avoid as much as possible spawning objects on runtime.
Instead try to reuse objects, pooling is a great concept on how to do that.
Branches/version control are only visible for users added to the project. Could you try creating a second, smaller, project that reproduces this issue again?
Hey there, I’m having the same issue. I was able to get no errors when creating the animated sprite, but it is still not appearing. The thing is, I think there may be a bug here. The reason being that I tried in the editor to create a normal sprite then turn it from simple to animated. After adding a new clip and giving it a sprite, nothing shows up. If you create an animated sprite instead in the editor, everything works fine. So I have a feeling it has something to do with that. Or I’m just messing up on my part, but any help would be appreciated. Oh and before I forget, here’s my code to create the animated sprite:
var animSpriteEntity = new pc.Entity(); // Create an Entity
animSpriteEntity.addComponent("sprite", { type: 'SPRITETYPE_ANIMATED', autoPlayClip: 'clip1' });
var addedClip = new pc.SpriteAnimationClip(animSpriteEntity.sprite, { name: 'clip1', fps: 4, loop: true, spriteAsset: this.testAnimSprite.resource });
animSpriteEntity.sprite.addClip(addedClip);
this.app.root.addChild(animSpriteEntity); // Add it to the Entity hierarchy
animSpriteEntity.sprite.play("clip1");