在Crafty.js中获取Sprite坐标

时间:2014-01-13 09:02:39

标签: javascript html coordinates craftyjs

我试图获得我的玩家角色所在的坐标,以便当他们从战斗中回来时#39;他们会在他们开始之前站在他们所在的地方,但是我使用了crafty.js附带的fourway功能,所以我无法看到任何使用的变量,有没有办法我可以或者我必须制作自己的运动事件?

这是我的代码:

Crafty.scene('Yr10Tp1', function () {
Location = 3;

Crafty.e('2RoomCorridor');
//Draw Entites in the game world
//Drawing Other Entities
Crafty.e('Borders');
//
//Temporary dirty fix, need to find way to store player co-ordinates
//

if (monsterBeat == false)
{
Crafty.e('Enemy').at(22, 10);
Crafty.e('Player').at(PlayerX, PlayerY);
}
else
{
Crafty.e('Player').at(22,10);
}

Crafty.e('TeleporterYR10TP1_1').at(16,1);
});

    Crafty.c('Player', {
init: function(){
this.requires('Actor, Color, Fourway, Collision, spr_player, SpriteAnimation,    Keyboard')

.color('none')
.fourway(3)
.stopOnSolids()
.onHit('NPC1Event', this.Level2)
.onHit('Enemy', this.attackEnemy)
.onHit('TeleporterHubWorld', this.Level2)
.onHit('TeleporterSpawn', this.Level2)
.onHit('TeleporterYR10TP1', this.Level2)
.onHit('TeleporterYR10TP1_1', this.Level2)
.onHit('TeleporterYR10TP1_1.1', this.Level2)
.onHit('TeleporterYR10TP1_1.2', this.Level2)
.onHit('TeleporterYR10TP2', this.Level2)
.onHit('TeleporterYR10TP3', this.Level2)
.onHit('TeleporterYR11TP1', this.Level2)
.onHit('TeleporterYR11TP2', this.Level2)
.onHit('TeleporterYR11TP3', this.Level2)
.animate('PlayerUp', 0, 0, 3)
.animate('PlayerDown', 0, 1, 3)
.animate('PlayerRight', 0, 2, 3)
.animate('PlayerLeft', 0, 3, 3)
.bind('KeyDown', function () { if (this.isDown('SPACE')) Crafty.scene('World'); })
.bind('KeyDown', function () { if (this.isDown('F')) Crafty.scene('BattleScreen'); });

// this makes the Sprite animate on player input, need to analyse before I complete
var animation_speed = 10;
this.bind('NewDirection', function(data) {
  if (data.x > 0) {
    this.animate('PlayerRight', animation_speed, -1);
  } else if (data.x < 0) {
    this.animate('PlayerLeft', animation_speed, -1);
  } else if (data.y > 0) {
    this.animate('PlayerUp', animation_speed, -1);
  } else if (data.y < 0) {
    this.animate('PlayerDown', animation_speed, -1);
  } else {
    this.stop();
  }
});
},

stopOnSolids: function() {
 this.onHit('Solid', this.stopMovement);

 return this;
},

attackEnemy: function(data) {
Enemy = data[0].obj;
Enemy.collect();
},

Level2: function(data) {
Teleporter1 = data[0].obj;
Teleporter1.collect();
monsterBeat = false;
},

Crafty.c('BattlePlayer', {
init:function() {
var MenuPos = 1;
var HealthPas = 100 + ',' + 100;
//Displays options for the first time with attack selected

this.requires('Actor, Color, spr_BattlePlayer, SpriteAnimation, Keyboard')
.color('none')
.bind('KeyDown', function () { if (this.isDown('SPACE')) Crafty.scene('World'); })
.bind('KeyDown', function () { if (this.isDown('ENTER')) HealthPas = this.BattleSelect(MenuPos, HealthPas);})
.bind('KeyDown', function () { if (this.isDown('S')) if (MenuPos < 3){MenuPos = MenuPos + 1}; this.MenuMove(MenuPos); })
.bind('KeyDown', function () { if (this.isDown('W')) if (MenuPos > 1) {MenuPos = MenuPos - 1}; this.MenuMove(MenuPos); });

Crafty.e('2D, DOM, Text')
.attr({ x: 100, y: 500 })
.text('N/A');

Crafty.e('2D, DOM, Text')
.attr({ x: 200, y: 150 })
.text(100);

Crafty.e('2D, DOM, Text')
.attr({ x: 200, y: 100 })
.text(100);

Crafty.e('AttackSel').at(3,8);
Crafty.e('DefendUnsel').at(3,13);
Crafty.e('RunUnsel').at(3,18);

var MenuPos = 1;    

},
//function for displaying what option is currently selected
MenuMove: function(MenuPos) {
switch (MenuPos)
{
case 1:
//Attack case
Crafty.e('AttackSel').at(3,8);
Crafty.e('DefendUnsel').at(3,13);
Crafty.e('RunUnsel').at(3,18);
Crafty.e('2D, DOM, Text')
.attr({ x: 100, y: 500 })
.text('N/A');
break;

case 2:
//Defend case
Crafty.e('AttackUnsel').at(3,8);
Crafty.e('DefendSel').at(3,13);
Crafty.e('RunUnsel').at(3,18);
Crafty.e('2D, DOM, Text')
.attr({ x: 100, y: 500 })
.text('N/A');
break;

case 3:
//Run case
Crafty.e('AttackUnsel').at(3,8);
Crafty.e('DefendUnsel').at(3,13);
Crafty.e('RunSel').at(3,18);
Crafty.e('2D, DOM, Text')
.attr({ x: 100, y: 500 })
.text('N/A');
break;

default:
//Incorrect input case
Crafty.e('AttackUnsel').at(3,8);
Crafty.e('DefendUnsel').at(3,13);
Crafty.e('RunUnsel').at(3,18);

Crafty.e('2D, DOM, Text')
.attr({ x: 100, y: 500 })
.text('N/A')
.textColor('#FF0000');
}
},

//function for carrying out battle options
//Within this function Num[0] represents players health and Nums[1] represents the  Enemy Health.
BattleSelect: function(MenuPos, HealthPas) {
var Nums = HealthPas.split(',');
Crafty.e('2D, DOM, Text')
.attr({ x: 200, y: 150 })
.text(Nums[0]);

switch (MenuPos)
{
case 1:
//Attack case
Nums[1] = Nums[1] - 20;
Crafty.e('2D, DOM, Text')
.attr({ x: 200, y: 100 })
.text(Nums[1]);
this.EndCheck(Nums[0], Nums[1]);
Nums[0] = Nums[0] - 10;
break;

case 2:
//Heal case
Nums[0] = Nums[0] - 1 + 21;
this.EndCheck(Nums[0], Nums[1]);
break;

case 3:
//Run case
//switch checks what room the player is in the transport them back there
switch (Location)
{
case 1:
Crafty.scene('World');
break;

case 2:
Crafty.scene('HubWorld');
break;

case 3:
Crafty.scene('Yr10Tp1');
break;

case 4:
Crafty.scene('Yr10Tp2');
break;

case 5:
Crafty.scene('Yr10Tp3');
break;

case 6:
Crafty.scene('Yr11Tp1');
break;

case 7:
Crafty.scene('Yr11Tp2');
break;

case 8:
Crafty.scene('Yr11Tp3');
break;

default:
Crafty.scene('World');
}

default:
this.EndCheck(Nums[0], Nums[1]);
Nums[0] = Nums[0] - 10;
}
HealthPas = Nums[0] + ',' + Nums[1];
return HealthPas;
},

//function to check for winning conditions
EndCheck : function(PlayerHealth, EnemyHealth)
{
var FightOver = false;

if (EnemyHealth < 1)
    {
    FightOver = true;
    monsterBeat = true;
    }
else if (PlayerHealth < 1)
    {
    FightOver = true;
    }

if (FightOver == true)
{
Crafty.e('2D, DOM, Text')
.attr({ x: 500, y: 150 })
.text('Victory!');
}

},
});

0 个答案:

没有答案