Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding hitareas

  • Loading branch information...
commit 611255d0cb00741a5cc79f3d2fe4b3296eac9718 1 parent ad3a120
@luizgustavolino luizgustavolino authored
View
43 Classes/Capn.js
@@ -2,6 +2,8 @@ var Capn = cc.Sprite.extend({
state: PM.PLAYER_STATE.UNKNOWN,
didDie: null,
+ normalHitArea: {w:30,h:70,dx:0,dy:-10},
+ crouchHitArea: {w:30,h:30,dx:0,dy:-40},
init: function(){
this._super();
@@ -11,10 +13,14 @@ var Capn = cc.Sprite.extend({
addFBFSpriteAnimationToCache("capn_down",3,.15,"capnDown");
addFBFSpriteAnimationToCache("capn_jump",4,.15,"capnJump");
addFBFSpriteAnimationToCache("capn_walk",4,.10,"capnWalk");
+ addFBFSpriteAnimationToCache("capn_hit", 1,.10,"capnHit");
this.scheduleUpdate();
this.wait();
+ this.setContentSize(cc.SizeMake(80,114));
+ addHitArea(this,this.normalHitArea.w,this.normalHitArea.h,this.normalHitArea.dx,this.normalHitArea.dy);
+
},
update:function(dt){
@@ -30,9 +36,12 @@ var Capn = cc.Sprite.extend({
if(this.state == PM.PLAYER_STATE.JUMP) return;
if(this.state == PM.PLAYER_STATE.DEAD) return;
+ if(this.state == PM.PLAYER_STATE.HIT) return;
if(this.state != PM.PLAYER_STATE.WALK_LEFT){
+
this.state = PM.PLAYER_STATE.WALK_LEFT;
+ editHitArea(this,this.normalHitArea.w,this.normalHitArea.h,this.normalHitArea.dx,this.normalHitArea.dy);
this.stopAllActions();
var animCache = cc.AnimationCache.sharedAnimationCache();
@@ -55,9 +64,12 @@ var Capn = cc.Sprite.extend({
if(this.state == PM.PLAYER_STATE.JUMP) return;
if(this.state == PM.PLAYER_STATE.DEAD) return;
+ if(this.state == PM.PLAYER_STATE.HIT) return;
if(this.state != PM.PLAYER_STATE.WALK_RIGHT){
+
this.state = PM.PLAYER_STATE.WALK_RIGHT;
+ editHitArea(this,this.normalHitArea.w,this.normalHitArea.h,this.normalHitArea.dx,this.normalHitArea.dy);
this.stopAllActions();
var animCache = cc.AnimationCache.sharedAnimationCache();
@@ -82,9 +94,12 @@ var Capn = cc.Sprite.extend({
if(this.state == PM.PLAYER_STATE.WALK_LEFT) return;
if(this.state == PM.PLAYER_STATE.WALK_RIGHT) return;
if(this.state == PM.PLAYER_STATE.DEAD) return;
+ if(this.state == PM.PLAYER_STATE.HIT) return;
if(this.state != PM.PLAYER_STATE.JUMP){
+
this.state = PM.PLAYER_STATE.JUMP;
+ editHitArea(this,this.normalHitArea.w,this.normalHitArea.h,this.normalHitArea.dx,this.normalHitArea.dy);
this.stopAllActions();
var animCache = cc.AnimationCache.sharedAnimationCache();
@@ -110,9 +125,12 @@ var Capn = cc.Sprite.extend({
if(this.state == PM.PLAYER_STATE.WALK_LEFT) return;
if(this.state == PM.PLAYER_STATE.WALK_RIGHT) return;
if(this.state == PM.PLAYER_STATE.DEAD) return;
+ if(this.state == PM.PLAYER_STATE.HIT) return;
if(this.state != PM.PLAYER_STATE.CROUCH){
+
this.state = PM.PLAYER_STATE.CROUCH;
+ editHitArea(this,this.crouchHitArea.w,this.crouchHitArea.h,this.crouchHitArea.dx,this.crouchHitArea.dy);
this.stopAllActions();
var animCache = cc.AnimationCache.sharedAnimationCache();
@@ -128,10 +146,12 @@ var Capn = cc.Sprite.extend({
if(this.state == PM.PLAYER_STATE.WALK_LEFT) return;
if(this.state == PM.PLAYER_STATE.WALK_RIGHT) return;
if(this.state == PM.PLAYER_STATE.DEAD) return;
+ if(this.state == PM.PLAYER_STATE.HIT) return;
if(this.state != PM.PLAYER_STATE.WAITING){
this.state = PM.PLAYER_STATE.WAITING
+ editHitArea(this,this.normalHitArea.w,this.normalHitArea.h,this.normalHitArea.dx,this.normalHitArea.dy);
this.stopAllActions();
var animCache = cc.AnimationCache.sharedAnimationCache();
@@ -161,6 +181,29 @@ var Capn = cc.Sprite.extend({
this.stopAllActions();
this.runAction(sequence);
+ },
+
+ hit: function(){
+
+ this.state = PM.PLAYER_STATE.HIT;
+
+ editHitArea(this,1,1,100000,1000000);
+ this.stopAllActions();
+
+ var animCache = cc.AnimationCache.sharedAnimationCache();
+ var animN = cc.Animate.create(animCache.animationByName("capnHit"));
+
+ var jump = cc.JumpTo.create(0.4, cc.ccp(this._position.x+20,202), 40, 1);
+
+ var action = cc.CallFunc.create(this, function(){
+ this.state = PM.PLAYER_STATE.UNKNOWN;
+ this.wait();
+ });
+
+ var spawn = cc.Spawn.create(jump,animN);
+ var sequence = cc.Sequence.create(spawn,action);
+ this.runAction(sequence);
+
}
});
View
13 Classes/GameScene.js
@@ -6,6 +6,7 @@ var GameScene = cc.Layer.extend({
sky: null,
clouds: null,
boat: null,
+ rebels: null,
init:function () {
@@ -37,17 +38,25 @@ var GameScene = cc.Layer.extend({
));
this.boat.runAction(upDownAction);
+ this.rebels = Rebels(this);
+ this.rebels.node.setPosition(cc.ccp(0,220));
+ this.rebels.node.setAnchorPoint(cc.ccp(0,0.5));
+ this.addChild(this.rebels.node);
+
this.waves = new Water();
this.waves.setPosition(cc.ccp(0, 0));
this.addChild(this.waves);
this.schedule(this.update);
+
return true;
},
update:function(dt){
+ this.rebels.update();
+
if(dpad.keys.up.state == dpad.stateKeyDown){
this.boat.capn.jump();
}else if(dpad.keys.down.state == dpad.stateKeyPressed){
@@ -61,6 +70,10 @@ var GameScene = cc.Layer.extend({
}
dpad.update();
+
+ if(rnd(100) == 4){
+ this.rebels.addRandomRebel();
+ }
}
});
View
10 Classes/PirateMutiny.js
@@ -17,4 +17,12 @@ PM.GAME_SCENE = {
GAME:1,
RESULTS:2,
HISCORE:3
-}
+}
+
+
+PM.REBEL_TYPE = {
+ UNKNOWN: 0,
+ BOTTLE: 1
+}
+
+PM.DEBUG = false;
View
90 Classes/Rebels.js
@@ -0,0 +1,90 @@
+var Rebels = function(_world){
+
+ var self = {};
+
+ self.world = _world;
+ self.node = new cc.Node();
+
+ self.update = function(){
+
+ var capn = self.world.boat.capn;
+ var capnBox = capn.hitArea.boundingBoxToWorld();
+ var list = self.node.getChildren();
+
+ for(var aRebelIndex in list){
+ var aRebel = list[aRebelIndex];
+ aRebel.update();
+
+ if(aRebel.live){
+ var hitArea = cc.Rect.CCRectIntersection(capnBox, aRebel.hitArea.boundingBoxToWorld());
+
+ if(hitArea.size.width > 0 && hitArea.size.height > 0){
+ aRebel.hit();
+ capn.hit();
+ }
+ }
+ }
+
+ }
+
+ self.addRandomRebel = function(){
+
+ var type = PM.REBEL_TYPE.BOTTLE;
+ var aRebel = null;
+
+ if(type == PM.REBEL_TYPE.BOTTLE){
+ aRebel = new BottleRebel.create();
+ }
+
+ if(aRebel){
+ self.node.addChild(aRebel);
+ }
+ }
+
+ return self;
+
+}
+
+
+var BaseRebel = cc.Sprite.extend({
+
+ live: true,
+
+ update: function(){
+ // do nothing
+ },
+
+ hit: function(){
+ this.live = false;
+ }
+
+});
+
+
+var BottleRebel = BaseRebel.extend({
+ update: function(){
+ if(this.live){
+ this._rotation += 10;
+ this.setPosition(cc.ccp(this._position.x+12, this._position.y));
+ }
+ },
+
+ hit: function(){
+ this.live = false;
+
+ this.stopAllActions();
+ var jump = cc.JumpBy.create(0.7, cc.ccp(120,-400), 220, 1);
+ this.runAction(jump);
+
+ }
+
+});
+
+BottleRebel.create = function (spriteFrameName) {
+ var pFrame = cc.SpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("bootle.png");
+ var sprite = new BottleRebel();
+ sprite.initWithSpriteFrame(pFrame);
+ sprite.setPosition(cc.ccp(-120, 90));
+ addHitArea(sprite,20,30,0,-10);
+ return sprite;
+};
View
19 Classes/SpriteUtils.js
@@ -53,3 +53,22 @@ var setupSpriteAnimation = function(imageName, frames){
return setupSpriteAnimationWithCustomDelay(imageName, frames, .15);
};
+
+var addHitArea = function(_sprite, w, h, _dx, _dy){
+ var hitArea = new cc.Sprite.create("Resources/hitArea.png", cc.RectMake(0, 0, w, h));
+ hitArea.setPosition(cc.ccp(_sprite.getContentSize().width/2+_dx, _sprite.getContentSize().height/2+_dy));
+ _sprite.addChild(hitArea);
+ _sprite.hitArea = hitArea;
+
+ if(!PM.DEBUG) hitArea.setOpacity(0);
+ else hitArea.setOpacity(120);
+}
+
+var editHitArea = function(_sprite, w, h, _dx, _dy){
+ var hitArea = _sprite.hitArea;
+ if(hitArea){
+ hitArea.setTextureRect(cc.RectMake(0,0,w,h));
+ hitArea.setPosition(cc.ccp(_sprite.getContentSize().width/2 + _dx, _sprite.getContentSize().height/2 + _dy));
+ }
+}
+
View
2  Classes/World.js
@@ -212,8 +212,6 @@ WaterTileBG.spriteWithSpriteFrameName = function (spriteFrameName) {
return sprite;
};
-
-
// WaterDrops
var addWaterDrops = function(origin, count){
View
BIN  Resources/hitArea.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
72 Resources/pm.plist
@@ -66,7 +66,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{352, 120}, {52, 66}}</string>
+ <string>{{304, 212}, {52, 66}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -88,7 +88,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{284, 440}, {52, 64}}</string>
+ <string>{{412, 120}, {52, 64}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -110,7 +110,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{400, 380}, {52, 62}}</string>
+ <string>{{400, 504}, {52, 62}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -132,7 +132,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{284, 506}, {52, 64}}</string>
+ <string>{{466, 114}, {52, 64}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -154,7 +154,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{304, 212}, {52, 66}}</string>
+ <string>{{284, 440}, {52, 66}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -181,6 +181,28 @@
<false/>
</dict>
+ <key>bootle.png</key>
+ <dict>
+ <key>aliases</key>
+ <array>
+
+ </array>
+ <key>spriteColorRect</key>
+ <string>{{14, 18}, {34, 72}}</string>
+ <key>spriteOffset</key>
+ <string>{1, -4}</string>
+ <key>spriteSize</key>
+ <string>{34, 72}</string>
+ <key>spriteSourceSize</key>
+ <string>{60, 100}</string>
+ <key>spriteTrimmed</key>
+ <true/>
+ <key>textureRect</key>
+ <string>{{240, 300}, {34, 72}}</string>
+ <key>textureRotated</key>
+ <false/>
+ </dict>
+
<key>capn_down01.png</key>
<dict>
<key>aliases</key>
@@ -247,6 +269,28 @@
<false/>
</dict>
+ <key>capn_hit01.png</key>
+ <dict>
+ <key>aliases</key>
+ <array>
+
+ </array>
+ <key>spriteColorRect</key>
+ <string>{{16, 22}, {58, 90}}</string>
+ <key>spriteOffset</key>
+ <string>{5, -10}</string>
+ <key>spriteSize</key>
+ <string>{58, 90}</string>
+ <key>spriteSourceSize</key>
+ <string>{80, 114}</string>
+ <key>spriteTrimmed</key>
+ <true/>
+ <key>textureRect</key>
+ <string>{{352, 120}, {58, 90}}</string>
+ <key>textureRotated</key>
+ <false/>
+ </dict>
+
<key>capn_idle01.png</key>
<dict>
<key>aliases</key>
@@ -264,7 +308,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{358, 188}, {60, 96}}</string>
+ <string>{{358, 212}, {60, 96}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -286,7 +330,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{62, 606}, {60, 94}}</string>
+ <string>{{346, 310}, {60, 94}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -308,7 +352,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{0, 606}, {60, 94}}</string>
+ <string>{{420, 186}, {60, 94}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -330,7 +374,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{346, 286}, {60, 92}}</string>
+ <string>{{476, 16}, {60, 92}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -396,7 +440,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{124, 596}, {60, 92}}</string>
+ <string>{{420, 282}, {60, 92}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -506,7 +550,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{204, 560}, {60, 96}}</string>
+ <string>{{400, 406}, {60, 96}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -660,7 +704,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{240, 16}, {110, 194}}</string>
+ <string>{{0, 206}, {110, 194}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -682,7 +726,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{0, 206}, {110, 194}}</string>
+ <string>{{240, 16}, {110, 194}}</string>
<key>textureRotated</key>
<false/>
</dict>
@@ -792,7 +836,7 @@
<key>spriteTrimmed</key>
<true/>
<key>textureRect</key>
- <string>{{406, 120}, {52, 28}}</string>
+ <string>{{408, 376}, {52, 28}}</string>
<key>textureRotated</key>
<false/>
</dict>
View
BIN  Resources/pm.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
4 cocos2d.js
@@ -37,7 +37,8 @@ cc.loadjs = function (filename) {
cc.Loader.shareLoader().preload([
{type:"plist", src:"Resources/pm.plist"},
- {type:"image", src:"Resources/pm.png"}
+ {type:"image", src:"Resources/pm.png"},
+ {type:"image", src:"Resources/hitArea.png"}
]);
}
};
@@ -56,4 +57,5 @@ cc.loadjs('Classes/Capn.js');
cc.loadjs('Classes/World.js');
cc.loadjs('Classes/SpriteUtils.js');
cc.loadjs('Classes/EngineUtils.js');
+cc.loadjs('Classes/Rebels.js');
Please sign in to comment.
Something went wrong with that request. Please try again.