How to make multiple variables equal a class?

15 ビュー (過去 30 日間)
Shane Gibbs
Shane Gibbs 2019 年 1 月 7 日
編集済み: OCDER 2019 年 1 月 7 日
Hello!
I need some help with trying to make multiple variables equal a 'class.'
I started using MatLab 2-3 months ago, so I am not very good at it.
I am currently making a menu-based RPG game, and I need to make an "equipped" screen.
What I am having trouble is making multiple variables = 1 "class"
For example...
Swords = (Bronze sword, Steel sword, Diamond Sword)
Helmet = (Bronze Helmet, Iron helmet, dark helmet)
Leggings = (Bronze Leggings, Iron Leggings, Stone Leggings)
and so on.
This way, I can prevent users from equipping a sword into a helmet slot, or prevent a legging item from equipping to a weapon slot.
I apologize if this is confusing.
Thank you so much! I will attach a copy of the game for assistance, or if you would like to play it.

採用された回答

OCDER
OCDER 2019 年 1 月 7 日
編集済み: OCDER 2019 年 1 月 7 日
Try the following testGame.m to see how to use object-oriented programming in Matlab. You'll need to define classes via classdef function. This link is helpful too: https://www.mathworks.com/help/matlab/matlab_oop/create-a-simple-class.html
Since most avatars have 2 hands, you'll need a separate function to decide what to do when you try to equip more items than the avatar can handle. Ex: equipping 2-hand sword + 1-hand shield shouldn't be allowed. You'll have to unequip the other hand, etc. This, I leave up the the game developer.
%testGame.m
P1 = Player('player1'); %Create new player of lv1 named player1
NewItem = Weapon('Sword Of Matlab', 10000, '2-hand'); %Make new item
P1.wear(NewItem); %Equip player with new item
NewItem = Armor('Shield of Functions', 10000, '1-hand'); %Make new item
P1.wear(NewItem); %Equip player with new item
P1.levelUp %Level up the player
P1.Level
% 2
P1.Armor
% Armor with properties:
% Name: 'Shield of Functions'
% ArmorPt: 10000
% Type: '1-hand'
P1.Weapon
% Weapon with properties:
% Name: 'Sword Of Matlab'
% Damage: 10000
% Type: '2-hand'

その他の回答 (1 件)

TADA
TADA 2019 年 1 月 7 日
編集済み: TADA 2019 年 1 月 7 日
Take A Look At classdef documentation
Also Worth mentioning That For Proper Polymorphic Behaviour In Matlab, your Base class Needs To Inherit matlab.mixin.Heterogeneous
Then You Can Also Use the isa Function To Test If An Object Is Of A Specific Type Or If It's Of A Subclass Of That Type

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by