logo的显示
private void logo() {
CCSprite logoSprite=CCSprite.sprite("image/popcap_logo.png");
//设置坐标位置 位于手机屏幕的中央
logoSprite.setPosition(cGSize.width/2,cGSize.height/2);
//添加到图层
this.addChild(logoSprite);
// 让logo执行动作
CCHide ccHide=CCHide.action();// 隐藏
CCDelayTime delayTime=CCDelayTime.action(1);//停留一秒钟
//在执行完动作后 通过反射运行loadwelcome方法
CCSequence ccSequence=CCSequence.actions(delayTime, delayTime,ccHide,delayTime, CCCallFunc.action(this, "loadWelcome"));
logoSprite.runAction(ccSequence);
}
欢迎界面的展示
public void loadWelcome(){
CCSprite bg=CCSprite.sprite("image/welcome.jpg");
bg.setAnchorPoint(0,0);
this.addChild(bg);
loading();
}
private void loading() {
CCSprite loading=CCSprite.sprite("image/loading/loading_01.png");
loading.setPosition(cGSize.width/2, 30);
this.addChild(loading);
CCAction animate = CommonUtils.getAnimate("image/loading/loading_%02d.png", 9, false);
loading.runAction(animate);
startSprite = CCSprite.sprite("image/loading/loading_start.png");
startSprite.setPosition(cGSize.width/2, 30);
startSprite.setVisible(false);// 暂时不可见
this.addChild(startSprite);
}
创建了序列帧的动作
public static CCAction getAnimate(String format, int num, boolean isForerver){
ArrayList<CCSpriteFrame> frames=new ArrayList<CCSpriteFrame>();
//String format="image/loading/loading_%02d.png";
for(int i=1;i<=num;i++){
CCSpriteFrame spriteFrame = CCSprite.sprite(String.format(format, i)).displayedFrame();
frames.add(spriteFrame);
}
CCAnimation anim=CCAnimation.animation("随便给", 0.2f, frames);
// 序列帧一般必须永不停止的播放 不需要永不停止播放,需要指定第二个参数 false
if(isForerver){
CCAnimate animate=CCAnimate.action(anim);
CCRepeatForever forever=CCRepeatForever.action(animate);
return forever;
}else{
CCAnimate animate=CCAnimate.action(anim,false);
return animate;
}
}
模拟后台加载数据
public Welcomelayer(){
//模拟数据的加载过程
new AsyncTask<Void,Void,Void>(){
//子线程之后执行的方法
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//将开始的图标设置为可见
startSprite.setVisible(true);
//开启点击开关
setIsTouchEnabled(true);
}
//在子线程中运行
@Override
protected Void doInBackground(Void... voids) {
SystemClock.sleep(6000);
return null;
}
}.execute();
init();
}
开始按钮的点击事件
@Override
public boolean ccTouchesBegan(MotionEvent event) {
//将安卓系统的点转换成coscod2d里面的点
CGPoint point=this.convertTouchToNodeSpace(event);
//开始精灵的大小
CGRect boundingBox = startSprite.getBoundingBox();
//判断点击的点是否再开始精灵里面
if(CGRect.containsPoint(boundingBox, point)){
// 处理点击事件
//System.out.println("我被点击了");
// 参数1 切换动画的时间 参数2 要切换的场景
CCScene scene=CCScene.node();
scene.addChild(new Menulayer());
CCFlipXTransition transition=CCFlipXTransition.transition(2, scene, 0);
CCDirector.sharedDirector().replaceScene(transition);//切换场景 ,参数 新的场景
}
return super.ccTouchesBegan(event);
}
图层切换动画总结
CCTransitionJumpZoom::transitionWithDuration(t, s);//跳跃式,本场景先会缩小,然后跳跃进来
CCTransitionFade::transitionWithDuration(t, s);//淡出淡入,原场景淡出,新场景淡入
CCTransitionFade::transitionWithDuration(t, s, ccWHITE);//如果上一个的函数,带3个参数,则第三个参数就是淡出淡入的颜色
CCTransitionFlipX::transitionWithDuration(t, s, kOrientationLeftOver);//x轴左翻
CCTransitionFlipX::transitionWithDuration(t, s, kOrientationRightOver);//x轴右翻
CCTransitionFlipY::transitionWithDuration(t, s, kOrientationUpOver);//y轴上翻
CCTransitionFlipY::transitionWithDuration(t, s, kOrientationDownOver);//y轴下翻
CCTransitionFlipAngular::transitionWithDuration(t, s, kOrientationLeftOver);//有角度转的左翻
CCTransitionFlipAngular::transitionWithDuration(t, s, kOrientationRightOver);//有角度转的右翻
CCTransitionZoomFlipX::transitionWithDuration(t, s, kOrientationLeftOver);//带缩放效果x轴左翻
CCTransitionZoomFlipX::transitionWithDuration(t, s, kOrientationRightOver);//带缩放效果x轴右翻
CCTransitionZoomFlipY::transitionWithDuration(t, s, kOrientationUpOver);//带缩放效果y轴上翻
CCTransitionZoomFlipY::transitionWithDuration(t, s, kOrientationDownOver);//带缩放效果y轴下翻
CCTransitionZoomFlipAngular::transitionWithDuration(t, s, kOrientationLeftOver);//带缩放效果/有角度转的左翻
CCTransitionZoomFlipAngular::transitionWithDuration(t, s, kOrientationRightOver);//带缩放效果有角度转的右翻
CCTransitionShrinkGrow::transitionWithDuration(t, s);//交错换
CCTransitionRotoZoom::transitionWithDuration(t, s);//转角换
CCTransitionMoveInL::transitionWithDuration(t, s);//新场景从左移入覆盖
CCTransitionMoveInR::transitionWithDuration(t, s);//新场景从右移入覆盖
CCTransitionMoveInT::transitionWithDuration(t, s);//新场景从上移入覆盖
CCTransitionMoveInB::transitionWithDuration(t, s);//新场景从下移入覆盖
CCTransitionSlideInL::transitionWithDuration(t, s);//场景从左移入推出原场景
CCTransitionSlideInR::transitionWithDuration(t, s);//场景从右移入推出原场景
CCTransitionSlideInT::transitionWithDuration(t, s);//场景从上移入推出原场景
CCTransitionSlideInB::transitionWithDuration(t, s);//场景从下移入推出原场景
以下三个需要检测opengl版本是否支持CCConfiguration::sharedConfiguration()->getGlesVersion() <= GLES_VER_1_0如果为真则为不支持
CCTransitionCrossFade::transitionWithDuration(t,s);//淡出淡入交叉,同时进行
CCTransitionRadialCCW::transitionWithDuration(t,s);//顺时针切入
CCTransitionRadialCW::transitionWithDuration(t,s);//逆时针切入
以下两个需要先设置摄像机,使用CCDirector::sharedDirector()->setDepthTest(true);
CCTransitionPageTurn::transitionWithDuration(t, s, false);//翻页,前翻
CCTransitionPageTurn::transitionWithDuration(t, s, true);//翻页,后翻
CCTransitionFadeTR::transitionWithDuration(t, s);//向右上波浪
CCTransitionFadeBL::transitionWithDuration(t, s);//向左下波浪
CCTransitionFadeUp::transitionWithDuration(t, s);//向上百叶窗
CCTransitionFadeDown::transitionWithDuration(t, s);//向下百叶窗
CCTransitionTurnOffTiles::transitionWithDuration(t, s);//随机小方块
CCTransitionSplitRows::transitionWithDuration(t, s);//按行切
CCTransitionSplitCols::transitionWithDuration(t, s);//按列切
菜单页面的显示
private void init() {
CCSprite sprite=CCSprite.sprite("image/menu/main_menu_bg.jpg");
sprite.setAnchorPoint(0,0);
this.addChild(sprite);
CCSprite normalSprite=CCSprite.sprite("image/menu/start_adventure_default.png");
CCSprite selectedSprite=CCSprite.sprite("image/menu/start_adventure_press.png");
// 菜单 参数1 默认显示的精灵 参数2 选中的时候显示的精灵 参数3 对象 参数4 方法
CCMenuItem items= CCMenuItemSprite.item(normalSprite, selectedSprite, this, "click");// 当被点击了 就会调用参数3对象中的 方法(参数4 方法名)
CCMenu menu=CCMenu.menu(items);
menu.setScale(0.5f); // 让菜单 缩放
menu.setPosition(cGSize.width / 2-25, cGSize.height / 2 - 110);
menu.setRotation(4.5f); // 设置了旋转的角度
this.addChild(menu);// 添加菜单
}
// 要想菜单能够正常反射该方法, 该方法必须有一个参数 Object类型
public void click(Object object){ // 参数 具体代表点击的是哪个条目
CommonUtils.changeLayer(new FightLayer());
}
加载地图
private void loadMap() {
//加载地图
map = CCTMXTiledMap.tiledMap("image/fight/map_day.tmx");
//设置锚点
map.setAnchorPoint(0.5f, 0.5f);
//设置地图的位置
CGSize contentSize = map.getContentSize();
map.setPosition(contentSize.width / 2, contentSize.height / 2);
this.addChild(map);
}
生成的地图在notepad中打开
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="14" height="6" tilewidth="46" tileheight="54">
<tileset firstgid="1" name="bk1" tilewidth="46" tileheight="54">
<image source="bk1.jpg" width="678" height="331"/>
</tileset>
<layer name="block" width="14" height="6">
<data encoding="base64" compression="zlib">
eJwNw4lSQQEAAMAXEZWEpKjooEShkhJdEkmS+v9vaXdml4IgCBl22YhRV4wZd9U11024YdJNU6bNuGXWbXPuuGvegnvue2DRkoceeeyJZSueembVc2vWvfDShk1bXnntjW1v7XjnvV0f7Nn30SefffHVgW8OfXfk2A8nfjr1y5nfzv1x4a9//gNAug3z
</data>
</layer>
<objectgroup name="road" width="14" height="6" visible="0">
<object x="473" y="96"/>
<object x="23" y="93"/>
<object x="474" y="148"/>
<object x="24" y="142"/>
<object x="473" y="205"/>
<object x="25" y="201"/>
<object x="472" y="251"/>
<object x="24" y="250"/>
<object x="474" y="306"/>
<object x="23" y="303"/>
</objectgroup>
<objectgroup name="tower01" width="14" height="6" visible="0">
<object x="84" y="94"/>
<object x="128" y="94"/>
<object x="171" y="94"/>
<object x="215" y="94"/>
<object x="257" y="94"/>
<object x="302" y="94"/>
<object x="346" y="94"/>
<object x="387" y="94"/>
<object x="433" y="94"/>
</objectgroup>
<objectgroup name="tower02" width="14" height="6" visible="0">
<object x="84" y="147"/>
<object x="128" y="147"/>
<object x="171" y="147"/>
<object x="215" y="147"/>
<object x="257" y="147"/>
<object x="302" y="147"/>
<object x="346" y="147"/>
<object x="387" y="147"/>
<object x="433" y="147"/>
</objectgroup>
<objectgroup name="tower03" width="14" height="6" visible="0">
<object x="84" y="203"/>
<object x="128" y="203"/>
<object x="171" y="203"/>
<object x="215" y="203"/>
<object x="257" y="203"/>
<object x="302" y="203"/>
<object x="346" y="203"/>
<object x="387" y="203"/>
<object x="433" y="203"/>
</objectgroup>
<objectgroup name="tower04" width="14" height="6" visible="0">
<object x="84" y="247"/>
<object x="128" y="247"/>
<object x="171" y="247"/>
<object x="215" y="247"/>
<object x="257" y="247"/>
<object x="302" y="247"/>
<object x="346" y="247"/>
<object x="387" y="247"/>
<object x="433" y="247"/>
</objectgroup>
<objectgroup name="tower05" width="14" height="6">
<object x="83" y="304"/>
<object x="127" y="304"/>
<object x="170" y="304"/>
<object x="214" y="304"/>
<object x="256" y="304"/>
<object x="301" y="304"/>
<object x="345" y="304"/>
<object x="386" y="304"/>
<object x="432" y="304"/>
</objectgroup>
<objectgroup name="zombies" width="14" height="6" visible="0">
<object x="529" y="85"/>
<object x="574" y="71"/>
<object x="528" y="148"/>
<object x="580" y="124"/>
<object x="620" y="148"/>
<object x="628" y="197"/>
<object x="579" y="192"/>
<object x="528" y="191"/>
<object x="532" y="246"/>
<object x="571" y="243"/>
<object x="621" y="239"/>
<object x="621" y="295"/>
<object x="577" y="305"/>
<object x="537" y="289"/>
</objectgroup>
</map>
解析地图
private void parerMap() {
ArrayList<CGPoint> zombiesPoits = new ArrayList<CGPoint>();
// 解析地图(对象层里面有road)
CCTMXObjectGroup objectGroupNamed = map.objectGroupNamed("zombies");
ArrayList<HashMap<String, String>> objects = objectGroupNamed.objects;
for (HashMap<String, String> hashMap : objects) {
int x = Integer.parseInt(hashMap.get("x"));
int y = Integer.parseInt(hashMap.get("y"));
//拿到我们画的每一个点
CGPoint cgPoint = ccp(x, y);
//讲所有的点放到集合里面
zombiesPoits.add(cgPoint);
}
}
僵尸的实体类
public class ShowZombies extends CCSprite {
//继承到的构造方法
public ShowZombies() {
super("image/zombies/zombies_1/shake/z_1_01.png");
//设置缩放
setScale(0.5f);
//设置锚点 在僵尸的两腿之间
setAnchorPoint(0.5f,0);//
//播放帧动画 让僵尸动起来
CCAction animate = CommonUtils.getAnimate("image/zombies/zombies_1/shake/z_1_%02d.png", 2, true);// 来回抖动
this.runAction(animate);
}
}
地图的移动
private void moveMap() {
int x = (int) (cGSize.width - map.getContentSize().width);
CCMoveBy moveBy = CCMoveBy.action(3, ccp(x, 0));
//运行完成后 通过反射运行loadContainer方法
CCSequence sequence = CCSequence.actions(CCDelayTime.action(4), moveBy, CCDelayTime.action(2),CCCallFunc.action(this, "loadContainer"));
map.runAction(sequence);
}
加载两个容器
// 加载两个容器
public void loadContainer() {
chose = CCSprite.sprite("image/fight/chose/fight_chose.png");
chose.setAnchorPoint(0, 1);
chose.setPosition(0, cGSize.height);// 设置位置是屏幕的左上角
this.addChild(chose);
choose = CCSprite.sprite("image/fight/chose/fight_choose.png");
choose.setAnchorPoint(0, 0);
this.addChild(choose);
loadShowPlant();
}
展示植物的实体类
public class ShowPlant {
static Map<Integer,HashMap<String,String>> db;
// 查询数据库 获取植物
static{
// 模拟了数据库
db=new HashMap<Integer, HashMap<String,String>>();
String format= "image/fight/chose/choose_default%02d.png";
for(int i=1;i<=9;i++){
HashMap<String, String> value=new HashMap<String, String>();
value.put("path",String.format(format, i));
value.put("sun", 50+"");
db.put(i, value);
}
}
//-----------------
private CCSprite showSprite;
private CCSprite bgSprite;
public ShowPlant(int id){
HashMap<String, String> hashMap = db.get(id);
String path = hashMap.get("path");
showSprite = CCSprite.sprite(path);
showSprite.setAnchorPoint(0,0);
//选完后的背景
bgSprite=CCSprite.sprite(path);
bgSprite.setOpacity(150);//设置半透明
bgSprite.setAnchorPoint(0,0);
}
public CCSprite getBgSprite() {
return bgSprite;
}
public CCSprite getShowSprite() {
return showSprite;
}
植物的选择和反选
@Override
public boolean ccTouchesBegan(MotionEvent event) {
// 需要把Android坐标系中的点 转换成Cocos2d坐标系中的点
CGPoint point = this.convertTouchToNodeSpace(event);
//分别获得已选择和可选择框的边界
CGRect chooseBox=choose.getBoundingBox();
CGRect choseBox = chose.getBoundingBox();
//判断点击的时候为可选择框
if (CGRect.containsPoint(chooseBox,point)){
//判断是否点击的是否为开始按钮
if(CGRect.containsPoint(start.getBoundingBox(), point)){
// 点击了一起来摇滚
ready();
//首先判断是否已经选择满了 或者 锁定了
}else if(selectPlants.size()<5&&!isLock)
//开始遍历植物 判断点击的是哪个植物的坐标点
for (ShowPlant plant :showPlatns) {
CGRect plantBox = plant.getShowSprite().getBoundingBox();
//判断是否点击的是这个植物
if (CGRect.containsPoint(plantBox,point)){
//将植物移动到上面的已选择框里面去
CCMoveTo moveTo = CCMoveTo.action(0.5f, ccp(75 + selectPlants.size() * 53, 255));
//移动完成后才能继续点击 走unlock 解锁
CCSequence sequence=CCSequence.actions(moveTo, CCCallFunc.action(this, "unlock"));
isLock=true;
plant.getShowSprite().runAction(sequence);
selectPlants.add(plant);
}
}
//可能是植物的反选
}else if(CGRect.containsPoint(choseBox,point)){
isDel=false;
for(ShowPlant plant:selectPlants){
CGRect selectPlantBox = plant.getShowSprite().getBoundingBox();
if(CGRect.containsPoint(selectPlantBox, point)){
//将植物移动到原来的位置
CCMoveTo moveTo=CCMoveTo.action(0.5f, plant.getBgSprite().getPosition());
plant.getShowSprite().runAction(moveTo);
selectPlants.remove(plant);// 走到这一步 确实代表反选植物了
isDel=true;
continue;
}
//将后面的植物往前面移动
if(isDel){
CCMoveBy ccMoveBy=CCMoveBy.action(0.5f, ccp(-53, 0));
plant.getShowSprite().runAction(ccMoveBy);
}
}
}
return super.ccTouchesBegan(event);
}