Damage or Heal based on the buttons you press
up vote
0
down vote
favorite
If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.
The project is here: https://github.com/austingaee/DamageHealth-Armor
Are there any improvements I can make?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
//Maximum Health
var barAmount : Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}
}
swift ios cocoa
add a comment |
up vote
0
down vote
favorite
If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.
The project is here: https://github.com/austingaee/DamageHealth-Armor
Are there any improvements I can make?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
//Maximum Health
var barAmount : Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}
}
swift ios cocoa
Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04
Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.
The project is here: https://github.com/austingaee/DamageHealth-Armor
Are there any improvements I can make?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
//Maximum Health
var barAmount : Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}
}
swift ios cocoa
If you press the damage button, it will damage armor before health. If you press the heal button, it will heal armor before health.
The project is here: https://github.com/austingaee/DamageHealth-Armor
Are there any improvements I can make?
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
//Maximum Health
var barAmount : Float = 0.0
override func viewDidLoad() {
super.viewDidLoad()
damageAmount = Float(self.armorBarView.frame.size.width) * 0.10
healAmount = Float(self.armorBarView.frame.size.width) * 0.05
barAmount = Float(self.armorBarView.frame.size.width)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func damageBar(_ sender: UIButton) {
//7.63 came from how much damage was left after 10 hits
if self.armorBarView.frame.size.width > 7.63 {
self.armorBarView.frame.size.width -= CGFloat(damageAmount)
} else if self.healthBarView.frame.size.width > 7.63 {
self.healthBarView.frame.size.width -= CGFloat(damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if self.armorBarView.frame.size.width < CGFloat(barAmount) {
self.armorBarView.frame.size.width += CGFloat(healAmount)
print(self.armorBarView.frame.size.width)
} else if self.healthBarView.frame.size.width < CGFloat(barAmount) {
self.healthBarView.frame.size.width += CGFloat(healAmount)
}
}
}
swift ios cocoa
swift ios cocoa
edited Aug 8 at 0:27
200_success
127k15148412
127k15148412
asked Aug 7 at 23:03
austingae
41613
41613
Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04
Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48
add a comment |
Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04
Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48
Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04
Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04
Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48
Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
not so much but some tiny changes:
DRY
i just would avoid all the self.healthBarView.frame.size.width
and make methods:
getWidth
setWidth
(orupdateWidth
because you set the diff)
Constants
the magic numbers like 0.1
0.05
10
i would define as constants so it can changed only at one place. 7.63 i would calculate
Naming
rename barAmount
to maxAmount
create minAmount
Float/CGFloat
the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth
else if
the else if block is not needed (same condition)
7.63
this is an value depending on your setup - it should also calculated so it not changes when you init it with different width
Resulting Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
// Min/Maximum Health
var minAmount: Float = 0.00
var maxAmount : Float = 0.0
// how much damage was left after this number of hits
let minAmountHits:Float = 10
let demageFactor: Float = 0.10
let healFactor: Float = 0.05
override func viewDidLoad() {
super.viewDidLoad()
initAmount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func initAmount(){
damageAmount = getWidth() * demageFactor
healAmount = getWidth() * healFactor
maxAmount = getWidth()
minAmount = maxAmount - minAmountHits * damageAmount
}
@IBAction func damageBar(_ sender: UIButton) {
if getWidth() > minAmount {
updateWidth(-damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if getWidth() < maxAmount {
updateWidth(healAmount)
}
}
func getWidth() -> Float {
return Float(self.armorBarView.frame.size.width)
}
func updateWidth(_ amount: Float) {
self.healthBarView.frame.size.width += CGFloat(amount)
print(self.armorBarView.frame.size.width)
}
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
not so much but some tiny changes:
DRY
i just would avoid all the self.healthBarView.frame.size.width
and make methods:
getWidth
setWidth
(orupdateWidth
because you set the diff)
Constants
the magic numbers like 0.1
0.05
10
i would define as constants so it can changed only at one place. 7.63 i would calculate
Naming
rename barAmount
to maxAmount
create minAmount
Float/CGFloat
the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth
else if
the else if block is not needed (same condition)
7.63
this is an value depending on your setup - it should also calculated so it not changes when you init it with different width
Resulting Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
// Min/Maximum Health
var minAmount: Float = 0.00
var maxAmount : Float = 0.0
// how much damage was left after this number of hits
let minAmountHits:Float = 10
let demageFactor: Float = 0.10
let healFactor: Float = 0.05
override func viewDidLoad() {
super.viewDidLoad()
initAmount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func initAmount(){
damageAmount = getWidth() * demageFactor
healAmount = getWidth() * healFactor
maxAmount = getWidth()
minAmount = maxAmount - minAmountHits * damageAmount
}
@IBAction func damageBar(_ sender: UIButton) {
if getWidth() > minAmount {
updateWidth(-damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if getWidth() < maxAmount {
updateWidth(healAmount)
}
}
func getWidth() -> Float {
return Float(self.armorBarView.frame.size.width)
}
func updateWidth(_ amount: Float) {
self.healthBarView.frame.size.width += CGFloat(amount)
print(self.armorBarView.frame.size.width)
}
}
add a comment |
up vote
0
down vote
not so much but some tiny changes:
DRY
i just would avoid all the self.healthBarView.frame.size.width
and make methods:
getWidth
setWidth
(orupdateWidth
because you set the diff)
Constants
the magic numbers like 0.1
0.05
10
i would define as constants so it can changed only at one place. 7.63 i would calculate
Naming
rename barAmount
to maxAmount
create minAmount
Float/CGFloat
the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth
else if
the else if block is not needed (same condition)
7.63
this is an value depending on your setup - it should also calculated so it not changes when you init it with different width
Resulting Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
// Min/Maximum Health
var minAmount: Float = 0.00
var maxAmount : Float = 0.0
// how much damage was left after this number of hits
let minAmountHits:Float = 10
let demageFactor: Float = 0.10
let healFactor: Float = 0.05
override func viewDidLoad() {
super.viewDidLoad()
initAmount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func initAmount(){
damageAmount = getWidth() * demageFactor
healAmount = getWidth() * healFactor
maxAmount = getWidth()
minAmount = maxAmount - minAmountHits * damageAmount
}
@IBAction func damageBar(_ sender: UIButton) {
if getWidth() > minAmount {
updateWidth(-damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if getWidth() < maxAmount {
updateWidth(healAmount)
}
}
func getWidth() -> Float {
return Float(self.armorBarView.frame.size.width)
}
func updateWidth(_ amount: Float) {
self.healthBarView.frame.size.width += CGFloat(amount)
print(self.armorBarView.frame.size.width)
}
}
add a comment |
up vote
0
down vote
up vote
0
down vote
not so much but some tiny changes:
DRY
i just would avoid all the self.healthBarView.frame.size.width
and make methods:
getWidth
setWidth
(orupdateWidth
because you set the diff)
Constants
the magic numbers like 0.1
0.05
10
i would define as constants so it can changed only at one place. 7.63 i would calculate
Naming
rename barAmount
to maxAmount
create minAmount
Float/CGFloat
the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth
else if
the else if block is not needed (same condition)
7.63
this is an value depending on your setup - it should also calculated so it not changes when you init it with different width
Resulting Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
// Min/Maximum Health
var minAmount: Float = 0.00
var maxAmount : Float = 0.0
// how much damage was left after this number of hits
let minAmountHits:Float = 10
let demageFactor: Float = 0.10
let healFactor: Float = 0.05
override func viewDidLoad() {
super.viewDidLoad()
initAmount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func initAmount(){
damageAmount = getWidth() * demageFactor
healAmount = getWidth() * healFactor
maxAmount = getWidth()
minAmount = maxAmount - minAmountHits * damageAmount
}
@IBAction func damageBar(_ sender: UIButton) {
if getWidth() > minAmount {
updateWidth(-damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if getWidth() < maxAmount {
updateWidth(healAmount)
}
}
func getWidth() -> Float {
return Float(self.armorBarView.frame.size.width)
}
func updateWidth(_ amount: Float) {
self.healthBarView.frame.size.width += CGFloat(amount)
print(self.armorBarView.frame.size.width)
}
}
not so much but some tiny changes:
DRY
i just would avoid all the self.healthBarView.frame.size.width
and make methods:
getWidth
setWidth
(orupdateWidth
because you set the diff)
Constants
the magic numbers like 0.1
0.05
10
i would define as constants so it can changed only at one place. 7.63 i would calculate
Naming
rename barAmount
to maxAmount
create minAmount
Float/CGFloat
the Float / CGFloat is realy needed? just put them also into getWidth and updateWidth
else if
the else if block is not needed (same condition)
7.63
this is an value depending on your setup - it should also calculated so it not changes when you init it with different width
Resulting Code
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var armorBarView: UIView!
@IBOutlet weak var healthBarView: UIView!
var damageAmount : Float = 0.0
var healAmount : Float = 0.0
// Min/Maximum Health
var minAmount: Float = 0.00
var maxAmount : Float = 0.0
// how much damage was left after this number of hits
let minAmountHits:Float = 10
let demageFactor: Float = 0.10
let healFactor: Float = 0.05
override func viewDidLoad() {
super.viewDidLoad()
initAmount()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func initAmount(){
damageAmount = getWidth() * demageFactor
healAmount = getWidth() * healFactor
maxAmount = getWidth()
minAmount = maxAmount - minAmountHits * damageAmount
}
@IBAction func damageBar(_ sender: UIButton) {
if getWidth() > minAmount {
updateWidth(-damageAmount)
}
}
@IBAction func healBar(_ sender: Any) {
if getWidth() < maxAmount {
updateWidth(healAmount)
}
}
func getWidth() -> Float {
return Float(self.armorBarView.frame.size.width)
}
func updateWidth(_ amount: Float) {
self.healthBarView.frame.size.width += CGFloat(amount)
print(self.armorBarView.frame.size.width)
}
}
edited Aug 22 at 20:18
answered Aug 22 at 20:04
muescha
1765
1765
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f201168%2fdamage-or-heal-based-on-the-buttons-you-press%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Is it okay to post a link to my github?
– austingae
Aug 7 at 23:04
Yes it is fine.
– Badhan Ganesh
Aug 9 at 10:48