cocos-2d 游戏开发
程序员文章站
2024-03-16 17:49:52
...
利用cocos-2d游戏引擎进行简单的游戏开发
levelLayer.java
package Layer;
import android.graphics.Point;
import android.util.Log;
import android.view.MotionEvent;
import org.cocos2d.actions.CCScheduler;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCRepeat;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCTMXObjectGroup;
import org.cocos2d.layers.CCTMXTiledMap;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.util.CGPointUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
/**
* Created by duboxuan on 2017/7/10.
*/
public class levelLayer extends BaseLayer {
CCTMXTiledMap map;
ArrayList<CGPoint> point;
CCSprite sprite;
CCSprite sprite_Pit;
ArrayList<CGPoint> pit;
private int index;
//速度
private int speed = 100;
public levelLayer() {
loadMap();
// loadSprite();
// move();
// loadpit();
// setIsTouchEnabled(true);
//调度器
CCScheduler scheduler = CCScheduler.sharedScheduler();
scheduler.schedule("attack",this,2f,false);
ArrayList<CCSpriteFrame> frame = new ArrayList<>();
for(int i=1;i<=8;i++){
CCSpriteFrame ccSpriteFrame = CCSprite.sprite("npc/npc"+i+".png").displayedFrame();
frame.add(ccSpriteFrame);
}
CCAnimation animation = CCAnimation.animation("XX",0.3F,frame);
CCAnimate animate = CCAnimate.action(animation,true);
CCRepeatForever forever = CCRepeatForever.action(animate);
sprite.runAction(forever);
}
//攻击的方法
public void attack(float f){
s1.setAnchorPoint(sprite.getPosition());
CCJumpTo j = CCJumpTo.action(1f.ccp(sprite.getPosition().x-50, sprite.getPosition().y-50),50,1);
CCSequence sequence = CCSequence.actions(j,CCCallFunc.action(this,"Done"));
s1.runAction(sequence);
this.addChild(s1);
}
//杀人
//炮弹的哦你攻击范围
public void Done(){
CGRect cgRect = CGRect.make(s.getPosition().x-50, s.getPosition().y-50,200,200 );
if(CGRect.containsRect(cgRect,sprite.getBoundingBox())){
Log.d("zz","Done:b被攻击了");
sprite.removeSelf();
}
}
//暂停的方法
public boolean ccTouchesBegan(MotionEvent event) {
CCSprite h = CCSprite.sprite("pit_h.png");
CGPoint cgPoint = convertTouchToNodeSpace(event);
boolean flag = true;
if(CGRect.containsPoint(cc.getBoundingBox(),cgPoint)){
if (flag) {
//暂停当前layer
this.onExit();
//声音暂停
SoundEngine.sharedEngine().pauseSound();
//添加一个新的Layer
flag = false;
} else {
this.onEnter();
SoundEngine.sharedEngine().pauseSound();
flag = true;
}
}
for (int i=0;i<pit.size();i++){
CGSize size = sprite_Pit.getBoundingBox().size;
CGRect rect = CGRect.make(pit.get(i).x,pit.get(i).y,size.getWidth(),size.getHeight());
if(CGRect.containsPoint(rect,cgPoint)){
Log.d("zz", "ccTouchesBegan: ");
h.setAnchorPoint(0,0);
h.setPosition(sprite_Pit.getPosition());
sprite_Pit.removeSelf();
this.addChild(h);
}else{
h.removeSelf();
this.addChild(sprite_Pit);
}
float p_x = pit.get(i).x;
float p_y = pit.get(i).y;
float x = rect.size.getWidth()/4;
float y = rect.size.getHeight()/4;
int temp=50;
CCSprite leader1 = CCSprite.sprite("leader/leader1.png");
leader1.setAnchorPoint(0,0);
leader1.setPosition(x+p_x-temp,y+p_y+temp);
this.addChild(leader1);
CCSprite leader2 = CCSprite.sprite("leader/leader2.png");
leader2.setAnchorPoint(0,0);
leader2.setPosition(x+p_x-temp,y+p_y+temp);
this.addChild(leader2);
CCSprite leader3 = CCSprite.sprite("leader/leader3.png");
leader3.setAnchorPoint(0,0);
leader3.setPosition(x+p_x-temp,y+p_y+temp);
this.addChild(leader3);
CCSprite leader4 = CCSprite.sprite("leader/leader4.png");
leader4.setAnchorPoint(0,0);
leader4.setPosition(x+p_x-temp,y+p_y+temp);
this.addChild(leader4);
CCSprite leaderbg = CCSprite.sprite("leader/leader_bg.png");
leaderbg.setAnchorPoint(0,0);
leaderbg.setPosition(x+p_x-temp,y+p_y+temp);
setScale((float) 0.5);
this.addChild(leaderbg);
}
return super.ccTouchesBegan(event);
}
CCSprite cc = CCSprite.sprite("");
private void loadMenu(){
cc.setAnchorPoint(0,0);
cc.setPosition(winSize.getWidth()-cc.getContentSize().getWidth(),winSize.getHeight()-40);
this.addChild(cc);
}
private void loadpit() {
pit = getPointByMap(map, "pit");
for(int i=0;i<pit.size();i++){
sprite_Pit = CCSprite.sprite("pit.png");
sprite_Pit.setAnchorPoint(0,0);
sprite_Pit.setPosition(pit.get(i));
this.addChild(sprite_Pit);
}
}
public void move() {
index++;
if(index<point.size()){
CCMoveTo moveto = CCMoveTo.action(CGPointUtil.distance(point.get(index-1), point.get(index))/speed,point.get(index));
CCMoveTo moveTo = CCMoveTo.action(2f,point.get(index));
CCSequence sequence = CCSequence.actions(moveto, CCCallFunc.action(this,"move"));
sprite.runAction(sequence);
}else if(index==point.size()){
removeSelf();
}
}
private void loadSprite() {
sprite = CCSprite.sprite("image 18300.png");
sprite.setAnchorPoint(0,0);
point = getPointByMap(map,"way");
sprite.setPosition(point.get(0));
this.addChild(sprite);
}
private void loadMap() {
map = CCTMXTiledMap.tiledMap("level.tmx");
this.addChild(map);
Log.d("zz", "loadMap: ww");
}
public ArrayList<CGPoint> getPointByMap(CCTMXTiledMap map,String name){
CCTMXObjectGroup cctmxObjectGroup = map.objectGroupNamed(name);
ArrayList<HashMap<String,String>> objects = cctmxObjectGroup.objects;
ArrayList<CGPoint> points = new ArrayList<>();
for(HashMap<String,String> HashMap:objects){
points.add(ccp(Integer.parseInt(HashMap.get("x")),Integer.parseInt(HashMap.get("y"))));
}
return points;
}
}
MainActivity.java
package com.example.d.myapplication;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.transition.Scene;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Objects;
import java.util.Set;
import java.util.StringTokenizer;
import Layer.*;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//创建surfaceview
// CCGLSurfaceView view=new CCGLSurfaceView(this);
//设置显示视图
// setContentView(view);
//获取导演对象,导演只有一个,你获取的是共享
// CCDirector director=CCDirector.sharedDirector();
//启动渲染线程
// director.attachInView(view);
//设置屏幕大小
// director.setScreenSize(800,480);
//设置屏幕方向
// director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
//设置显示FPS
// director.setDisplayFPS(true);
//设置每秒多少帧
// director.setAnimationInterval(1/30);
// CCScene scene=CCScene.node();
// scene.addChild(new WeloceLayer());
// scene.addChild(new levelLayer());
// director.runWithScene(scene);
//游戏暂停
// CCGLSurfaceView view = new CCGLSurfaceView(this);
// setContentView(view);
// CCDirector ccDirector = CCDirector.sharedDirector();
// ccDirector.attachInView(view);
// ccDirector.setScreenSize(800,400);
// ccDirector.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
// ccDirector.setDisplayFPS(true);
// CCScene sence = CCScene.node();
// sence.addChild(sence);
// sence.addChild(new levelLayer());
// ccDirector.runWithScene(sence);
// protected void onPause(){
//游戏继续
//Activity 销毁,游戏结束
}
}
BaseLayer.java
package Layer;
import android.util.Log;
import com.example.d.myapplication.R;
import org.cocos2d.actions.base.CCAction;
import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItem;
import org.cocos2d.menus.CCMenuItemSprite;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGSize;
/**
* Created by duboxuan on 2017/7/10.
*/
public class BaseLayer extends CCLayer {
public CGSize winSize = CCDirector.sharedDirector().getWinSize();
public BaseLayer() {
//加载声音
SoundEngine sound = SoundEngine.sharedEngine();
sound.playSound(CCDirector.sharedDirector().theApp, R.raw.liren,true);
CCSprite bg = CCSprite.sprite("welcome.jpg");
bg.setAnchorPoint(0,0);
bg.setScaleX(winSize.getWidth()/bg.getContentSize().getWidth());
bg.setScaleY(winSize.getHeight()/bg.getContentSize().getHeight());
this.addChild(bg);
CCSprite logo = CCSprite.sprite("image 3293.png");
logo.setAnchorPoint(0,0);
logo.setPosition(0,50);
logo.setScale(0.5);
this.addChild(logo);
CCAction jumpTo = CCJumpTo.action(1f,ccp(50,winSize.getHeight()-220),10,1);
logo.runAction(jumpTo);
CCSprite normalSprite = CCSprite.sprite("image 3262.png");
CCSprite selectedSprite = CCSprite.sprite("image 3266.png");
CCMenuItemSprite menuItemSprite = CCMenuItemSprite.item(normalSprite,selectedSprite,this,"click");
CCMenu menu = CCMenu.menu(menuItemSprite);
menu.setAnchorPoint(0,0);
menu.setPosition(400,200);
this.addChild(menu);
}
public void click(Object obj){
int zz = Log.d("zz", "click: 点击我");
CCScene scene = CCScene.node();
scene.addChild(new levelLayer());
CCDirector.sharedDirector().replaceScene(scene);
}
}
上一篇: 二维瓦片网格边界检测