Sign in to follow this  
scofieldly

auto shot the bullet to locked sprite

Recommended Posts

hi all, 

I am a newbie to phaser, currently I am doing a shotting game, and I want to make a auto shot feature.

 

the gun should lock a moving sprite first and shot until the sprite die, so I wonder is there any function that can do the 'lock target' function, thanks

Share this post


Link to post
Share on other sites

This is very rough, but you will want something like this. The lock function will fire when you shoot a bullet setting a variable to false. In your move / update function, check against this value to stop moving it the sprite is locked.

A timer is created for 2 seconds, which on completion will trigger the unlock function, releasing the character. You could go a step further and change the timer to an event callback for the shooting animation, but that's optional. Hope this helps!

shootBullet() {
    this.lock();
    let bullet = new Bullet(x,y,texture); // instantiation of bullet class
    this.game.time.events.add(Phaser.Timer.SECOND * 2, this.unlock, this);
}

lock() {
    this.locked = true;
}

unlock() {
    this.locked = false;
}

move() {
    if(!this.locked) {
        ... // move your char here if not locked
    }
}

 

Share this post


Link to post
Share on other sites
12 hours ago, megmut said:

This is very rough, but you will want something like this. The lock function will fire when you shoot a bullet setting a variable to false. In your move / update function, check against this value to stop moving it the sprite is locked.

A timer is created for 2 seconds, which on completion will trigger the unlock function, releasing the character. You could go a step further and change the timer to an event callback for the shooting animation, but that's optional. Hope this helps!


shootBullet() {
    this.lock();
    let bullet = new Bullet(x,y,texture); // instantiation of bullet class
    this.game.time.events.add(Phaser.Timer.SECOND * 2, this.unlock, this);
}

lock() {
    this.locked = true;
}

unlock() {
    this.locked = false;
}

move() {
    if(!this.locked) {
        ... // move your char here if not locked
    }
}

hi megmut,

thanks for your reply, sorry for my misunderstanding explanation, I did not mean when the bullet collide the target will lock the target. I mean the target will keep moving and the gun will adjust the angle to aim the target and keep shotting. do you have any idea about this feature, thanks.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.