-1

I'm trying to do a little game with Vuejs. The problem is that I want an inventory, and add objects to it. Each object must have its own action, for example potions heal. So I'm trying to do the following, but I think this cannot be done with Vue, but I do not find a solution.

new Vue({
el: '#app',
data: {
    playerHealth: 100,
    enemyHealth: 100,
    invClicked: false,
    gameStarted: false,
    logs: [],
    specialAttacks: [],
    inventory: {
        potion: {
            amount: 1,
            img: 'images/potion.svg',
            action: function(){
                this.playerHealth = this.playerHealth + 25;
            }
        }
    }
},
computed: {
    eHealthColor: function() {
        if (this.enemyHealth < 25){
            return 'red';
        }
    },
    pHealthColor: function() {
        if (this.playerHealth < 25){
            return 'red';
        }
    },
    numberOfPlayers: function() {
        return 2;
    }
},
methods: {
    attack: function(event) {
        let dealedDamage = (Math.floor(Math.random() * (5 - 1)) + 1);
        let damageTaken = (Math.floor(Math.random() * (3 - 1)) + 1);

        if(this.enemyHealth > dealedDamage){
            this.enemyHealth = this.enemyHealth - dealedDamage;
            this.logs.unshift(`PLAYER HITS ENEMY FOR ${dealedDamage}`);
        } else {
            this.enemyHealth = 0;
            alert('YOU WON!')
        }

        if(this.playerHealth > damageTaken){
            this.playerHealth = this.playerHealth - damageTaken;
            this.logs.unshift(`ENEMY HITS PLAYER FOR ${damageTaken}`);
        } else{
            this.playerHealth = 0;
            alert('YOU LOST!');
        }
    },
    findItem: function() {
        var item = this.inventory.potion;
        return item;
    }
}
});

The action method is not working, I've tried with this keyword and with double quotes and do notwork either. In html I'm calling by:

<div class="inv-item position-relative d-flex justify-content-center" @click="findItem().action">

Another problem is the amount, I would like that the function action would reduce the amount, but I can't refer to that object. Thanks!

1

2 Answers 2

0

Avoid using .find and make your Array and Object

inventory: {
  'potion': {
    ...
  }
}

To access the potion action:

inventory.potion
Sign up to request clarification or add additional context in comments.

Comments

0

You may try to call action:

<div class="inv-item position-relative d-flex justify-content-center" @click="findItem().action()">Action</div>

or use object directly:

<div class="inv-item position-relative d-flex justify-content-center" @click="inventory.potion.action">Action</div>

2 Comments

Yeah, but still not working. I think the error is in the function, I do not know very well what to return
@RaúlS add method action() { this.findItem().action() } and call it inside click event like @click="action". And add a code this.playerHealth = this.playerHealth + 25; this.amount++;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.