본문 바로가기

Cocos2d

Cocos2d Android : 3장 . CCParticleSystem 사용법

반응형
//CCParticleSystem 사용법 입니다(expo.png 파일을 만들어서 asset에 넣어주심 됩니다)
//Cocos2d 라이브러리에 있는 CCParticleExplosion 클래스를 약간(?) 변경했습니다..
//터치 이벤트에서  schedule을 돌려야  CCParticleSystem  정상적으로 동작되는데 이유는 잘 모르겠네요..
//폭팔효과 외에도 CCParticleFireworks , CCParticleGalaxy ,CCParticleRain 등 다양한 효과가 있으니
   테스트 해보시길...^^;

StartLayer.java
-------------------------------------------------------------------------------------------------
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.types.CGPoint;

import android.view.MotionEvent;

public class StartLayer extends CCLayer {
CCSprite expoSprite;

public StartLayer() {
this.setIsTouchEnabled(true);
setBackGround();
}

private void setBackGround() {
expoSprite = CCSprite.sprite("expo.png");
}

public static CCScene scene() {
CCScene scene = CCScene.node();
CCLayer layer = new StartLayer();
scene.addChild(layer);
return scene;
}
CGPoint startLocation;
@Override
public boolean ccTouchesBegan(MotionEvent event) {
startLocation = CCDirector.sharedDirector().convertToGL(
CGPoint.make(event.getX(), event.getY()));
if (expoSprite.getBoundingBox().contains(startLocation.x,
startLocation.y)) {
 this.removeChild(expoSprite, false);
schedule("explode", 0.1f);

}

return super.ccTouchesBegan(event);
}
public void explode(float dt)
    {
unschedule("explode");
        CCParticleSystem emitter;
        emitter = CCParticleExplo.node();
        emitter.setTexture(CCTextureCache.sharedTextureCache().addImage("expo.png"));
        emitter.setAutoRemoveOnFinish(true);
        emitter.setPosition(startLocation.x, startLocation.y);
        emitter.setSpeedVar(400);
        emitter.setStartSize(20f);
        emitter.setEndSize(10f);
        emitter.setDuration(2.0f);
        emitter.setSpeed(200);
        addChild(emitter);
    }

}
------------------------------------------------------------------------------------------------

CCParticleExplo.java
-------------------------------------------------------------------------------------------------
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.particlesystem.CCQuadParticleSystem;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccBlendFunc;

public class CCParticleExplo extends CCQuadParticleSystem {

    public static CCParticleSystem node() {
        return new CCParticleExplo();
    }

    protected CCParticleExplo() {
        this(20);
    }

    protected CCParticleExplo(int p) {
        super(p);
   
// duration
duration = 0.1f;
// emitterMode = kCCParticleModeGravity;

// Gravity Mode: gravity
this.setGravity(CGPoint.ccp(0,0));
// Gravity Mode: speed of particles
speed = 70;
speedVar = 40;
// Gravity Mode: radial
radialAccel = 0;
radialAccelVar = 0;
// Gravity Mode: tagential
tangentialAccel = 0;
tangentialAccelVar = 0;
// angle
angle = 90;
angleVar = 360;
// emitter position
CGSize winSize = CCDirector.sharedDirector().winSize();
this.setPosition(CGPoint.ccp(winSize.width/2, winSize.height/2));
posVar = CGPoint.zero();
// life of particles
life = 5.0f;
lifeVar = 2;
// size, in pixels
//startSize = 15.0f;
//startSizeVar = 10.0f;
//endSize = kCCParticleStartSizeEqualToEndSize;

// emits per second
emissionRate = totalParticles/duration;
// color of particles
startColor.r = 0.12f;
startColor.g = 0.25f;
startColor.b = 0.76f;
startColor.a = 1.0f;
startColorVar.r = 0.0f;
startColorVar.g = 0.0f;
startColorVar.b = 0.0f;
startColorVar.a = 0.0f;
endColor.r = 0.0f;
endColor.g = 0.0f;
endColor.b = 0.0f;
endColor.a = 1.0f;
endColorVar.r = 0.0f;
endColorVar.g = 0.0f;
endColorVar.b = 0.0f;
endColorVar.a = 0.0f;
        texture = CCTextureCache.sharedTextureCache().addImage("expo.png");

        // additive
        blendAdditive = false;
    }

@Override
public ccBlendFunc getBlendFunc() {
// TODO Auto-generated method stub
return null;
}

@Override
public void setBlendFunc(ccBlendFunc blendFunc) {
// TODO Auto-generated method stub
}

}
 ------------------------------------------------------------------------------------------------ 
반응형