What version of Cocos Creator?
What is the problem you are experiencing?
Cocos creator 2.4.5
The brick actually should not go outside the wall but it is going outside both brick and wall are rigid body and physics box Collider and brick includes touch functions for movement I have enabled collision logic as well in Cocos creator
Did you get any insight of my problem?
Can you please help me out
this is a code of green brick
// var touched;
let sound = {
BUTTON : “Texture/sound/gamewin”,
}
cc.Class({
extends: cc.Component,
properties: {
},
// LIFE-CYCLE CALLBACKS:
onLoad: function() {
cc.director.getCollisionManager().enabled = true;
cc.director.getCollisionManager().enabledDebugDraw = true;
// this.touchingNumber = 0;
cc.director.getPhysicsManager().enabled = true;
this.registerEvent();
// this.touchEnd = true;
},
init(gamecontroller){
this.gamecontroller = gamecontroller;
},
onCollisionEnter: function (other) {
},
onCollisionStay: function (other) {
},
onCollisionExit: function () {
},
Hide_Window(){
this.playSound(sound.BUTTON);
cc.tween(this.node)
.to(0.6,{ scale:0.2, opacity:0 },{ easing: "elasticOut" })
.call(() => { this.node.active = false; })
.start();
},
onBeginContact(contact, self, other) {
switch (other.tag) {
case 1:
this.gamecontroller.onMoveBlockContactTerminateBox(self.node, other.node);
this.Hide_Window();
break;
}
},
registerEvent(){
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
},
onTouchStart: function(event){
// console.log(event.target.x);
// console.log(event);
},
onTouchEnd: function(event){
var we_pos = event.target.x;
// console.log(we_pos);
if(we_pos > -160 & we_pos <= -76 ){
var action = cc.speed(cc.moveTo(4, -160, 69));
// execute the action
this.node.runAction(action);
// console.log(1);
}
else if(we_pos > -76 & we_pos <= 87){
var action = cc.speed(cc.moveTo(4, 0, 69));
this.node.runAction(action);
// console.log(2);
// this.node.stopAction(action);
}
else if(we_pos >= 0 & we_pos <= 87){
var action = cc.speed(cc.moveTo(4, 0, 69));
this.node.runAction(action);
// console.log(3);
}
else if(we_pos > 87 ){
var action = cc.speed(cc.moveTo(4, 170, 69));
this.node.runAction(action);
// console.log(4);
}
// this.touchEnd = true;
},
onTouchCancel: function(event){
// console.log("cc.Node.EventType.TOUCH_CANCEL called");
},
onTouchMove: function(event){
// var w_pos = event.getLocation();
var delta = event.getDelta();
this.node.x += delta.x;
},
playSound : function(name){
cc.loader.loadRes(name, cc.AudioClip, function (err, clip) {
var audioID = cc.audioEngine.playEffect(clip, false);
});
},
});
this is a code of white brick
var touched;
cc.Class({
extends: cc.Component,
properties: {
},
// LIFE-CYCLE CALLBACKS:
onLoad: function() {
cc.director.getCollisionManager().enabled = true;
cc.director.getCollisionManager().enabledDebugDraw = true;
// this.touchingNumber = 0;
cc.director.getPhysicsManager().enabled = true;
this.registerEvent();
this.touchEnd = true;
},
init(gamecontroller){
this.gamecontroller = gamecontroller;
},
onCollisionEnter: function (other) {
console.log('on collision enter');
touched = true;
this.touchEnd = false;
},
onCollisionStay: function (other) {
// console.log('on collision stay');
},
onCollisionExit: function () {
},
registerEvent(){
this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this);
this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
},
onTouchStart: function(event){
},
onTouchEnd: function(event){
var we_pos = event.target.y;
// var we_pos2 = this.node.getPosition();
// console.log(we_pos2.y);
if(we_pos < 141.5 & we_pos < 70 & we_pos > -11 ){
var action = cc.speed(cc.moveTo(4, 81, -11));
// execute the action
this.node.runAction(action);
}
else if(we_pos > -11){
var action = cc.speed(cc.moveTo(4, 81, 155));
// execute the action
// console.log(2);
this.node.runAction(action);
}
else if(we_pos > 155){
var action = cc.speed(cc.moveTo(4, 81, 155));
// execute the action
// console.log(2);
this.node.runAction(action);
}
else if(we_pos < -11 & we_pos > -96 ){
var action = cc.speed(cc.moveTo(4, 81, -11));
this.node.runAction(action);
}
else if(we_pos < -96 ){
var action = cc.speed(cc.moveTo(4, 81, -162.5));
this.node.runAction(action);
// console.log(4);
}
else if(we_pos > -166 ){
var action = cc.speed(cc.moveTo(4, 81, -11));
this.node.runAction(action);
// console.log(5);
}
this.touchEnd = true;
// console.log(1);
},
onTouchCancel: function(event){
console.log('touch cancelled');
},
onTouchMove: function(event){
if(touched === true & !this.touchEnd){
console.log(1);
var we_pos1 = event.target.y;
// console.log(we_pos1);
// y1 = we_pos1 - 6;
// console.log(y1);
// var action = cc.moveTo(81, y1);
// this.node.runAction(action);
// this.touchEnd = true;
}
else{
console.log(2);
var delta = event.getDelta();
this.node.y += delta.y;
}
},
});
Any help is greatly appreciated.
Can you provide a simple demo?