Sprite usando CCAnimation, reiniciar animação de um botão

139 views
Skip to first unread message

Edjane Guimaraes

unread,
Aug 12, 2013, 1:12:01 AM8/12/13
to desenvolvimento-de...@googlegroups.com

Olá, segui o livro e ele me ajudou muito no entendimento do desenvolvimento de jogos, e no uso do Cocos2D.

Mas esbarrei em um problema, que não estou conseguindo resolver. Estou criando um jogo, que quando se clica em uma imagem ela realize uma animação e quando essa animação terminar ela volte ao estado inicial.

Consegui configurar a animação, criei um Sprite e usando CCAnimation adicionei as imagens como frames.

Já consegui fazer também que quando clico no sprite ele carregue a animação e após isso, volte ao primeiro frame.

O que esta acontecendo é que não estou conseguindo implementar uma forma que ao clicar na imagem novamente ele reinicie a animação. Já tentei algumas formas de fazer isso funcionar, mas não obtive êxito.


Segue código, usando como base aprendizado do livro

//configuração do sprite atraves de
    public class BotaoSprite extends CCLayer{
        private CCSprite botaoImagem;
        private ButtonDelegate delegate;

        public BotaoSprite (String bi, int valor) {
            this.setIsTouchEnabled(true);
           
            //para retirar a extensão e fazer com q o for ache os arquivos, CRIAR um método para controlar isso
            String teste = bi.substring(0,5);
           
            this.botaoImagem = CCSprite.sprite(bi);
           
            CCAnimation botaoAnimado= CCAnimation.animation("", 1f);
           
            for (int i = 1; i<=valor; i++){
                    botaoAnimado.addFrame(teste + i + ".png");
                   }
                    //configurado para voltar para o primeiro frame
                    CCAnimate botaoAcao = CCAnimate.action(5, botaoAnimado, true);
                    botaoImagem.runAction(botaoAcao);           

            addChild(this.botaoImagem);
           
            //ligando o touch
            this.setIsTouchEnabled(true);
        }
       
        public void setDelegate(ButtonDelegate sender) {
            this.delegate = sender;
        }
       
        @Override
        protected void registerWithTouchDispatcher() {
            CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, 0, false);
        }
       
        @Override
        public boolean ccTouchesBegan(MotionEvent event) {
            CGPoint touchLocation = CGPoint.make(event.getX(), event.getY());
            touchLocation = CCDirector.sharedDirector().convertToGL(touchLocation);
            touchLocation = this.convertToNodeSpace(touchLocation);
           
            // Verifica toque no botão
                if (CGRect.containsPoint(
                        this.botaoImagem.getBoundingBox(), touchLocation)) {
                        delegate.buttonClicked(this);
                }
            return true;
        }
}

//classe onde insiro os dados do BotaoSprite
public class BotoesDasCores extends CCLayer implements ButtonDelegate{
    private BotaoSprite vermelho;
    public BotoesDasCores(){
        //informar que o touch ira ficar ligado
        this.setIsTouchEnabled(true);
               
        //indicar a imagem do botão através da classe Asset e quantidade frames
        this.vermelho = new BotaoSprite(Assets.TESTE1, 2);
               
        //CHAMADA AO MÉTODO PARA ALOCAR OS OBJETOS NA POSIÇÃO CORRETA
        setPosicao();
               
        addChild(vermelho);
                               
        this.vermelho.setDelegate(this);
    }
           
            //METODO USADO PARA POSICIONAR OS ELEMENTOS NA POSIÇÃO CORRETA
            public void setPosicao(){
                vermelho.setPosition(screenResolution(CGPoint.ccp( screenWidth() - 55 , screenHeight() - 55)));
            }

            @Override
            public void buttonClicked(Botao sender) {
                // TODO Auto-generated method stub
                //sobrescrever esse método somente para a tela de menus
            }

            @Override
            public void buttonClicked(BotaoSprite botaoSprite) {
                // TODO Auto-generated method stub
                if (botaoSprite.equals(this.vermelho)) {
                    System.out.println("Button clicked: botão vermelho!");
                   
                    //carregando o arquivo de som para essa imagem
                    SoundEngine.sharedEngine().playEffect(CCDirector.sharedDirector().getActivity(), R.raw.red);
                }
            }
        }





Reply all
Reply to author
Forward
0 new messages