三消游戏中随机排列图片位置,和点击图片消失下落
2016-07-05 15:10
162 查看
var GameData ={
W_NUM:8,
H_NUM:8,
}
var HelloWorldLayer = cc.Layer.extend({
jiehsou: null,
sp:null,
arr:null,
ctor:function(){
this._super();
this._init()
var that = this
cc.eventManager.addCustomListener("diaoluo",function(event){
从精灵类中抛出一个自定义事件,传 出他的arrIndex(点击的图标的下标) 消失的图片. 将下标传入一个
下落方法
that.jiehsou = event.getUserData()
cc.log("xiabiaio", that.jiehsou.x,that.jiehsou.y)
var i = that.jiehsou.x
for(var j = that.jiehsou.y - 1; j >= 0; j--){
if(that.arr[i][j] == null)
continue;
that.arr[i][j].fallDown()
var a = that.arr[i][j].arrIndex
that.arr[a.x][a.y] = that.arr[i][j]
that.arr[i][j] = null
}
})
},
_init:function(){
this.arr = []
for(var i =0 ;i<GameData.W_NUM;i++){
this.arr [i] = []
for(var j =0 ;j<GameData.H_NUM;j++){
this.sp = new Cell(i,j)
this.sp.setPosition(200+(40*i),cc.winSize.height-20-(40*j))
this.addChild(this.sp)
this.sp.setArrIndex(i,j)
this.arr[i][j] = this.sp
}
}
},
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
Cell= cc.Sprite.extend({
type:0,
listener1:null,
i: null,
j: null,
arrIndex: null,
labei: null,
ctor:function(i , j) {
this._super();
this.j = j;
this._init()
this._lear()
this.scheduleUpdate()
this.arrIndex = {
x: i,
y: j,
}
},
update: function (){
this.labei.setString(this.arrIndex.x+","+this.arrIndex.y,"",10)
},
//随机生成图片
_init:function(){
this.type =parseInt(1+Math.random()*4);
this.initWithFile("res/pic"+this.type+".png")
},
setArrIndex:function(i,j){ //设置下标
this.labei =new cc.LabelTTF(i+","+j,"",10)
this.labei.enableStroke(cc.color(0,0,0,255),1)
this.labei.setPosition(10,10)
this.addChild(this.labei)
},
// 下落方法
fallDown: function(){
var move = cc.moveBy(2, 0, -40)
this.runAction(move)
this.arrIndex.y++
},
//点击事件
_lear:function(){
var that = this;
this.listener1 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget()
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize()
var rect = cc.rect(0, 0, s.width, s.height)
if (cc.rectContainsPoint(rect, locationInNode) ) {
that.removeFromParent(true) //消除点击的块
cc.eventManager.dispatchCustomEvent("diaoluo", that.arrIndex)
}
},
onTouchMoved: function (touch, event) {
},
onTouchEnded: function (touch, event) {
}
})
cc.eventManager.addListener(this.listener1, this)
}
})
W_NUM:8,
H_NUM:8,
}
var HelloWorldLayer = cc.Layer.extend({
jiehsou: null,
sp:null,
arr:null,
ctor:function(){
this._super();
this._init()
var that = this
cc.eventManager.addCustomListener("diaoluo",function(event){
从精灵类中抛出一个自定义事件,传 出他的arrIndex(点击的图标的下标) 消失的图片. 将下标传入一个
下落方法
that.jiehsou = event.getUserData()
cc.log("xiabiaio", that.jiehsou.x,that.jiehsou.y)
var i = that.jiehsou.x
for(var j = that.jiehsou.y - 1; j >= 0; j--){
if(that.arr[i][j] == null)
continue;
that.arr[i][j].fallDown()
var a = that.arr[i][j].arrIndex
that.arr[a.x][a.y] = that.arr[i][j]
that.arr[i][j] = null
}
})
},
_init:function(){
this.arr = []
for(var i =0 ;i<GameData.W_NUM;i++){
this.arr [i] = []
for(var j =0 ;j<GameData.H_NUM;j++){
this.sp = new Cell(i,j)
this.sp.setPosition(200+(40*i),cc.winSize.height-20-(40*j))
this.addChild(this.sp)
this.sp.setArrIndex(i,j)
this.arr[i][j] = this.sp
}
}
},
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
});
Cell= cc.Sprite.extend({
type:0,
listener1:null,
i: null,
j: null,
arrIndex: null,
labei: null,
ctor:function(i , j) {
this._super();
this.j = j;
this._init()
this._lear()
this.scheduleUpdate()
this.arrIndex = {
x: i,
y: j,
}
},
update: function (){
this.labei.setString(this.arrIndex.x+","+this.arrIndex.y,"",10)
},
//随机生成图片
_init:function(){
this.type =parseInt(1+Math.random()*4);
this.initWithFile("res/pic"+this.type+".png")
},
setArrIndex:function(i,j){ //设置下标
this.labei =new cc.LabelTTF(i+","+j,"",10)
this.labei.enableStroke(cc.color(0,0,0,255),1)
this.labei.setPosition(10,10)
this.addChild(this.labei)
},
// 下落方法
fallDown: function(){
var move = cc.moveBy(2, 0, -40)
this.runAction(move)
this.arrIndex.y++
},
//点击事件
_lear:function(){
var that = this;
this.listener1 = cc.EventListener.create({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var target = event.getCurrentTarget()
var locationInNode = target.convertToNodeSpace(touch.getLocation());
var s = target.getContentSize()
var rect = cc.rect(0, 0, s.width, s.height)
if (cc.rectContainsPoint(rect, locationInNode) ) {
that.removeFromParent(true) //消除点击的块
cc.eventManager.dispatchCustomEvent("diaoluo", that.arrIndex)
}
},
onTouchMoved: function (touch, event) {
},
onTouchEnded: function (touch, event) {
}
})
cc.eventManager.addListener(this.listener1, this)
}
})
相关文章推荐
- Java学习笔记(十八):序列化
- <context:component-scan>使用说明
- TabLayout与ViewPager的使用
- PAT 1057. Stack
- 经典计算机视觉论文笔记——《Network in Network》
- [置顶] 手把手搭建一个SpringMVC+ibatis 工程
- Paths on a Grid(poj 1942)
- 全屏API接口
- php中的错误处理
- nginx codeigniter 配置
- 微信支付和微信第三方登录的冲突问题
- 关于解决eclipse被墙无法下载一些插件和更新的问题
- yarn模式运行spark
- yarn模式运行spark
- KafkaOffsetMonitor安装&&测试
- java使用BufferedImage操作合成图片
- mysql连接数据库
- vi 使用记录
- Activity
- u-boot向linux内核传递启动参数