Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by cweed

Hey, I'm loving impact so far, but the lack of real mouse / clicking support has been hurting me a bit. I'm trying to make a tower defense game where the only input is mouse clicks.

I'm having trouble achieving the functionality I want, and I believe my problem is in these two functions in my mousePos entity. This is an entity I created as per dominic's spec in another thread to handle clicking via the check method (http://impactjs.com/forums/help/how-to-handle-clicking).

These methods are attempting to handle my 4 cases (cases labeled in-comment)

//called every time mouse collides with a clickable object
	check: function( other ) {
		this.checked = "on";
		if (this.clickTimer.delta() >= 0){ //timer fixes multiple clicking bug
			if (typeof(other.hover) == 'function'){
				other.hover();
				if (this.isClicking) {
                                        // #1
					ig.game.theHud.unSelectAll();
					other.clicked();
				}
			}
			else if (this.isClicking && typeof(other.clicked) == 'function'){
				if (other.name == "Upgrade Button"){
                                        // #2
					other.clicked()
				}
				else {
                                        // #3
					ig.game.theHud.unSelectAll();
					other.clicked();
				}
			}
			this.clickTimer.reset();
		}
		this.parent(other);
	},

and then the update function:

update: function() {
		
		this.parent();
		this.checked = "off"
		//ig.game.message = this.checked
		this.pos.x = ig.input.mouse.x
		this.pos.y = ig.input.mouse.y
		this.isClicking = ig.input.pressed('mouse1');
		
		
		
		if (this.checked == "off" && this.isClicking == true){
			// # 4
			ig.game.theHud.unSelectAll();
			ig.game.theHud.towerDisplayState = "notActive";
		}
	}

Case #1 - This case is specifically for the towers. So it checks if the entity the mouse is hovering over has a hover method (only towers do), and then calls that repeatedly as the mouse stays hovered over. If you click on that tower, it deselects every other tower, and then calls that tower's clicked() function (which "selects" it).

Else (if it's being checked against an entity with no hover() method - namely the "build tower" and "upgrade" buttons): if you're clicking and the button you're checking has a clicked method:
case #2 - and it's name is the "Upgrade Button" then call it's clicked() method
case #3 (if it's not named "Upgrade Button": deselect everything else, then call it's clicked() method.

Cases 2 and 3 exist because I want the tower to stay selected only when the upgrade button is clicked - not when any other button is pressed.

And then case # 4 exists because I want you to deselect any tower if you click on an empty spot. I can't include it in check() because that method is only called when the mouse is colliding with an entity - not when it's over an empty space. I'm attempting to use the this.checked state to determine if check() has been called that frame (and to ignore the method if it has), but it's not working. Clicking on the upgrade button (case #2) still results in everything being deselected. If I comment out the if statement in update(), this doesn't happen, and case #2 works as I intend it. So my deduction is that the if statement in update() is overriding check(). Is there another way to do it?

This is my first programming project of this size, so it's possible I've bitten off more than I can chew and am going about this backwards. However as far as I can tell my problem is that I do not understand the correct way to tell whether check() has been called that frame.

I hope this was clear enough for you to read through and offer help, thanks.

edit: edited to give the explanation more context and to flesh out my actual question. This will make it a little more readable, hopefully

1 decade ago by OMATASE

I believe the order it runs is check, then update. Where are these code snippets found in your code? Are they in different entities? Knowing which entities these snippets were in and how they were related would help for diagnosing the problem.

If both snippets are from the same entity, it looks like although you're turning checked "on" in check, you're turning it back off again in update causing the unselectAll() method to fire as long as the mouse is being clicked.

1 decade ago by OMATASE

Actually now that I look at it I don't remember which order those two methods are called. My code works regardless of the order as it turns out.

1 decade ago by cweed

Thank you for the reply. These are both in the mousePos entity. Do you mean you do something similar and it works for you?
Page 1 of 1
« first « previous next › last »