使用cocos2d-android做最简单的飞机大战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014293306/article/details/52049969

什么是cocos2d-android

用一句话简单来说,cocos2d-android就是cocos引擎的java版本的API。

优缺点

其优点是容易使用和入门,不需要使用其他语言交叉编译,其开发和调试相比cocos2d-x等其他版本都比较方便。当然缺点也就显而易见–不能进行跨平台开发。另外随着触控科技加大对c++/lua/js的投入,cocos2d-android也面临着缺乏维护而没落的危机。

如何引入到自己的项目中

  • 下载cocos2d-android.jar直接导入
  • 如果找不到jar包的话,可以到github上直接下载源代码,然后自己导出包,或者直接使用源代码。

MainActivity

package com.example.cocos2ddemo;

import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;

import com.masonchen.cocos.FirstLayer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    private CCDirector director;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CCGLSurfaceView surfaceView=new CCGLSurfaceView(this);

        setContentView(surfaceView);
        //  程序只能有一个导演
        director = CCDirector.sharedDirector();
        director.attachInView(surfaceView);// 开启线程

        director.setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);// 设置游戏方向 水平
        director.setDisplayFPS(true);//是否展示帧率
    //  director.setAnimationInterval(1.0f/30);// 锁定帧率  指定一个帧率  向下锁定 
        director.setScreenSize(720,1280);//设置屏幕的大小   可以自动屏幕适配 

        CCScene ccScene=CCScene.node();// 为了api 和cocos-iphone 一致 
        ccScene.addChild(new FirstLayer());//场景添加了图层 
        director.runWithScene(ccScene);//  运行场景

    }
    @Override
    protected void onResume() {
        super.onResume();
        director.resume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        director.onPause();
        //director.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        director.end();// 游戏结束了
    }

}

GameLayer

package com.masonchen.cocos;

import java.util.ArrayList;
import java.util.List;

import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.instant.CCCallFuncN;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCMoveBy;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCRepeat;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCNode;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.types.CGPoint;

import com.badlogic.gdx.physics.box2d.World;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;

public class FirstLayer extends CCLayer {
    static final int kTagSprite = 1;
    static final int manSpeed = 500;
    private CCSprite[] fires;// 子弹
    private CCSprite[] enemys;// 敌机

    private int numberOfFire;
    private int numberOfEnemy;

    public FirstLayer() {
        init();
    }

    /*
     * 初始化背景,子弹,敌机,英雄的精灵对象
     */
    private void init() {

        CCSprite background = CCSprite
                .sprite("ui/shoot_background/background.png");
        background.setScale(3.0);
        this.addChild(background);
        CCSprite sprite = CCSprite.sprite("ui/shoot/hero1.png");
        this.addChild(sprite, 0, kTagSprite);

        fires = new CCSprite[10];//循环回收利用,只使用10个子弹,避免浪费内存
        enemys = new CCSprite[10];//和上面同理
        for (int i = 0; i < 10; i++) {
            fires[i] = CCSprite.sprite("ui/shoot/bullet1.png");
            fires[i].setPosition(-100, -100);
            addChild(fires[i]);
        }
        for (int i = 0; i < 10; i++) {
            enemys[i] = CCSprite.sprite("ui/shoot/enemy1.png");
            enemys[i].setPosition(-200, -200);
            addChild(enemys[i]);
        }

        numberOfFire = 0;
        numberOfEnemy = 0;
        schedule("fire", 0.2f);// 英雄飞机开火的调度器,每隔0.2s发射一颗子弹
        schedule("createEnemy", 0.5f);// 创建敌机调度器,每隔0.5s创建一个敌机
        scheduleUpdate(); // 默认调度器,进行子弹和敌机的碰撞检测
        isTouchEnabled_ = true;//设置为可触摸
    }

    @Override
    public boolean ccTouchesBegan(MotionEvent event) {
        CGPoint point = new CGPoint();
        point.set(event.getX(), event.getY());
        CGPoint convertedLocation = CCDirector.sharedDirector().convertToGL(
                point);

        CCNode sprite = getChildByTag(kTagSprite);
        sprite.stopAllActions();

        sprite.runAction(CCMoveTo.action(getDistance(convertedLocation)
                / manSpeed,
                CCNode.ccp(convertedLocation.x, convertedLocation.y)));
        return super.ccTouchesBegan(event);
    }

    public float getDistance(CGPoint touchPoint) {
        CCNode sprite = getChildByTag(kTagSprite);
        CGPoint nowPoint = sprite.getPosition();
        float dx = nowPoint.x - touchPoint.x;
        float dy = nowPoint.y - touchPoint.y;
        return (float) Math.sqrt(dx * dx + dy * dy);
    }

    public void update(float dt) {
        // Log.i("", "默认调度器");

        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                if (enemys[i].getBoundingBox().contains(
                        fires[j].getPosition().x, fires[j].getPosition().y)) {
                    enemys[i].stopAllActions();

                    fires[j].stopAllActions();
                     enemys[i].setPosition(-100,-100);
                    fires[j].setPosition(-200, -100);
                    break;
                    // return;
                }
            }
        }
    }

    public void enemysCallback() {


    }

    public void fire(float dt) {
        fires[numberOfFire]
                .setPosition(getChildByTag(kTagSprite).getPosition());
        fires[numberOfFire].setVisible(true);
        fires[numberOfFire].runAction(CCMoveBy.action((float) 1.5,
                CCNode.ccp(0, 1380)));

        numberOfFire++;
        if (numberOfFire >= 10)
            numberOfFire -= 10;
    }

    public void createEnemy(float dt) {

        enemys[numberOfEnemy].setPosition((float) (720 * Math.random()), 1280);
        enemys[numberOfEnemy].setScale(1.5);
        enemys[numberOfEnemy].setVisible(true);
        enemys[numberOfEnemy].runAction(CCMoveBy.action((float) 3.5,
                CCNode.ccp(0, -1400)));

        numberOfEnemy++;
        if (numberOfEnemy >= 10)
            numberOfEnemy -= 10;
    }
}

运行效果

这里写图片描述

阅读更多
换一批

没有更多推荐了,返回首页