Skip to main content

News

Topic: Necromancy: Problem with SetPersonSpriteset() (Read 2585 times) previous topic - next topic

Necromancy: Problem with SetPersonSpriteset()
I want to make the bullet change its spriteset to the obtruding person's spriteset so you "steal" that entity's 'soul'. I could swear my codding was right but I'm getting an error message. I don't understand whats wrong. The error message is attached.


Code: (javascript) [Select]

var bullets = [];
var bullet_num = 0; // how many bullets where shot
var bullet_distance = 32; //distance of bullets journey in pixels
var bullet_sprite = "bullet.rss"

//Bullet is an Object with arguments
function Bullet(name, start_x, start_y, vel_x, vel_y)
{
    this.name = name;
    this.startX = start_x;
    this.startY = start_y;
    this.xVelocity = vel_x;
    this.yVelocity = vel_y;
    this.speed = 1; //general speed of bullet
 
    CreatePerson(name, bullet_sprite, true); // name of this bullet;
    SetPersonX(name, start_x);
    SetPersonY(name, start_y);
   
    switch(direction) {
        case direction: SetPersonDirection(name,direction);break; //north -y
    }
}

//creates and positions bullet according to player direction
function CreateBullet(player)
{
    var x = 0, y = 0;
    switch(direction) {
        case "north": y = -1;break; //north -y
        case "south": y = 1;break; //south +y
        case "west": x = -1;break; //west -x
        case "east": x = 1;break;   //east x
    }
bullet_num++;
//creat a new instance for Bullet(name, start_x, start_y, vel_x, vel_y) and store inside bullets array
    bullets.push(new Bullet("bullet: " + bullet_num, GetPersonX(player) + x * 32, GetPersonY(player) + y * 32, x, y));
}

Bullet.prototype.update = function() {
    var x = GetPersonX(this.name) + this.xVelocity * this.speed;
    var y = GetPersonY(this.name) + this.yVelocity * this.speed;
    SetPersonX(this.name, x);
    SetPersonY(this.name, y);
    QueuePersonCommand(this.name,COMMAND_ANIMATE,true);
   
   
    // pythagoras theorem a^2 + b^2 = c^2
    var dx = x - this.startX;
    var dy = y - this.startY;
    var dist = Math.sqrt(dx*dx + dy*dy);
   
    if (dist >= bullet_distance) {
        this.dead = true;
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "") {
       bullet_sprite = GetPersonSpriteset(obstruction);
        SetPersonSpriteset(this.name,bullet_sprite)
        }
    }
}

function UpdateBullets()
{
    for (var i = 0; i < bullets.length; ++i) {
        bullets[i].update();
        if (bullets[i].dead) { DestroyPerson(bullets[i].name); bullets.splice(i, 1); i--; }
    }
}



Line 22
Code: (javascript) [Select]

CreatePerson(name, bullet_sprite, true);



  • Last Edit: July 13, 2013, 04:00:23 pm by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Necromancy: Problem with SetPersonSpriteset()
Reply #1
That's not how you use a switch statement, in fact it's not needed at all! Also where are you getting the direction? You'll need to add it to the constructor and then pass it into creation like below. Also, as far as detecting the changing spriteset, CreatePerson takes a string (eg: 'bullet.rss') for the creation. GetPersonSpriteset() returns a spriteset object (which I almost prefer). So you just need to change the spriteset after creation, as also seen below:

Code: (javascript) [Select]

function Bullet(name, start_x, start_y, vel_x, vel_y, direction)
{
    this.name = name;
    this.startX = start_x;
    this.startY = start_y;
    this.xVelocity = vel_x;
    this.yVelocity = vel_y;
    this.speed = 1; //general speed of bullet
 
    CreatePerson(name, 'bullet.rss', true); // name of this bullet;
    SetPersonSpriteset(name, bullet_sprite);
    SetPersonX(name, start_x);
    SetPersonY(name, start_y);

    SetPersonDirection(name, direction);
}

function CreateBullet(player)
{
    var x = 0, y = 0;
    switch(direction) {
        case "north": y = -1;break; //north -y
        case "south": y = 1;break; //south +y
        case "west": x = -1;break; //west -x
        case "east": x = 1;break;   //east x
    }
    bullet_num++;
    //creat a new instance for Bullet(name, start_x, start_y, vel_x, vel_y) and store inside bullets array
    bullets.push(new Bullet("bullet: " + bullet_num, GetPersonX(player) + x * 32, GetPersonY(player) + y * 32, x, y, direction));
}


Your code wold have worked had GetPersonSpriteset() return a filename. If there are any other problems, do ask!
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Necromancy: Problem with SetPersonSpriteset()
Reply #2
 ??? Is there a way I can get a string of at least the Persons name?

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Necromancy: Problem with SetPersonSpriteset()
Reply #3
You have been it's called 'obstruction' in here:

Code: (javascript) [Select]

    if (dist >= bullet_distance) {
        this.dead = true;
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "") {
            bullet_sprite = GetPersonSpriteset(obstruction);
            SetPersonSpriteset(this.name, bullet_sprite);
        }
    }
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Necromancy: Problem with SetPersonSpriteset()
Reply #4
facepalm

I was actually going to try this before but I just thought it was "too simple"

Code: (javascript) [Select]

    if (dist >= bullet_distance) {
        this.dead = true;
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "") {
        bullet_sprite = obstruction + ".rss"
        }
    }
}


I guess simplicity is always the right way. Thanks for pointing out the obvious, it flew right under my nose  ;)

Re: Necromancy: Problem with SetPersonSpriteset()
Reply #5
but how would I check if the bullet is obstructing itself so as not to create a sprite that does not exist out of its name?

I tried...

Code: (javascript) [Select]
if (obstruction != "" && obstruction != this.name)

then

Code: (javascript) [Select]
if (obstruction != "" && obstruction != "bullet:"+bullet_num)

then

Code: (javascript) [Select]
if (obstruction != "" && obstruction != "bullet:"+bullet_num++)

then

Code: (javascript) [Select]
if (obstruction != "" && obstruction != "bullet:"+bullet_num--)


The bullet still collides with itself if you shoot fast enough. Setting the speed higher temporarily fixes this BUT the collision doesn't even register an obstructing entity, for some reason if the speed of distance is higher the bullet just swoops right over the Person without getting a name.
  • Last Edit: July 14, 2013, 05:16:37 am by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Necromancy: Problem with SetPersonSpriteset()
Reply #6
Code: (javascript) [Select]

if (obstruction != "" && obstruction.indexOf("bullet:") < 0)


Would do the trick. IndexOf gets the position in a string where the word starts. If it is less than zero it doesn't exist. That means it won't collide with sprites that have 'bullet:' in their name. Some of these code problems can only be solved with experience I'm afraid. I have a great knowledge of the JS and Sphere API's and am able to use both advantageously. It just takes time + trial and error. All new tricks you learn you just pick up and use later. Like using the name to figure out a spriteset, it may just work but not always.

You'll need a spriteset for each different name. You can use string.substring() to make sure you get the exact name.

Code: (javascript) [Select]

var name = string.substring(0, 5); // if the name is expected to have 5 characters in it before numbers start.


The above grabs a portion of the string, so names like "hello1" and "hello55" both return "hello". You can read up on all of the JS functions here: https://developer.mozilla.org/en/JavaScript/Reference
  • Last Edit: July 14, 2013, 05:14:21 am by Radnen
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Necromancy: Problem with SetPersonSpriteset()
Reply #7
Ok I'll be sure to start studying javascript and sphere's API along with some more mechanics and maths concepts relevant to game proggramming starting tomorrow.

I have a problem with the bullet_distance, when its set to something above my tile-size (32pixels) it doesn't get the obstructing persons name for some reason and just swoops over unaffected. I don't get what's causing this because x and y are already set to update themselves and (this.name,x,y) should be good enough for collision detection. I tried it with floating point co-ordinates for x and y but setx and sety seem to have to be integers but this should not be a problem because even when the speed is low (1) it still swoops over and doesn't get an obstructing name. when bullet_distance is 32 and even when the speed is ridiculously fast like 8 or 10 it still gets the obstructing name and the collision is checked.The bullet_distance code should have absolutely no effect on the obstruction collision detection, I don't get what's going on.

Here is the code once again:

Code: (javascript) [Select]

var bullets = [];
var bullet_num = 0; // how many bullets where shot
var bullet_distance = 32; //distance of bullets journey in pixels
var bullet_sprite = "ghost1.rss"

//Bullet is an Object with arguments
function Bullet(name, start_x, start_y, vel_x, vel_y)
{
    this.name = name;
    this.startX = start_x;
    this.startY = start_y;
    this.xVelocity = vel_x;
    this.yVelocity = vel_y;
    this.speed = 1;
    CreatePerson(name,bullet_sprite, true);
    SetPersonX(name, start_x);
    SetPersonY(name, start_y);
    SetPersonDirection(name,direction); 
}

//creates and positions bullet according to player direction
function CreateBullet(player)
{
    var x = 0, y = 0;
    switch(direction) {
        case "north": y = -1;break; //north -y
        case "south": y = 1;break; //south +y
        case "west": x = -1;break; //west -x
        case "east": x = 1;break;   //east x
    }
bullet_num++;
//creat a new instance for Bullet(name, start_x, start_y, vel_x, vel_y) and store inside bullets array
    bullets.push(new Bullet("shot"+bullet_num, GetPersonX(player) + x * 32, GetPersonY(player) + y * 32, x, y));
}

Bullet.prototype.update = function() {
    var x = GetPersonX(this.name) + this.xVelocity * this.speed;
    var y = GetPersonY(this.name) + this.yVelocity * this.speed;
    SetPersonX(this.name, x);
    SetPersonY(this.name, y);
    QueuePersonCommand(this.name,COMMAND_ANIMATE,true);
   
   
    // pythagoras theorem a^2 + b^2 = c^2
    var dx = x - this.startX;
    var dy = y - this.startY;
    var dist = Math.sqrt(dx*dx + dy*dy);
   
    if (dist >= bullet_distance) {
        this.dead = true;
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "" && obstruction.indexOf("shot") < 0){
        bullet_sprite = obstruction + ".rss"
        }
    }
}

function UpdateBullets()
{
    for (var i = 0; i < bullets.length; ++i) {
        bullets[i].update();
        if (bullets[i].dead) { DestroyPerson(bullets[i].name); bullets.splice(i, 1); i--; }
    }
}

  • Last Edit: July 14, 2013, 05:43:28 am by Xenso

  • Radnen
  • [*][*][*][*][*]
  • Senior Staff
  • Wise Warrior
Re: Necromancy: Problem with SetPersonSpriteset()
Reply #8
Alright, try this where you put the distance.
Code: (javascript) [Select]

    if (dist >= bullet_distance) this.dead = true;
    else {
        var obstruction = GetObstructingPerson(this.name, x, y);
        if (obstruction != "" && obstruction.indexOf("shot") < 0) {
            bullet_sprite = obstruction + ".rss"
        }
    }


Before it only detected the obstruction for entities exactly or greater than the distance away. This should cover all 32 pixels.
If you use code to help you code you can use less code to code. Also, I have approximate knowledge of many things.

Sphere-sfml here
Sphere Studio editor here

Re: Necromancy: Problem with SetPersonSpriteset()
Reply #9
Ah yes, I'll take more notice with if statements next time.
Thanks once again  ;)