How to detect UIImageView Collisions - iphone

I am wondering why my code doesn't work. I want my ball (UIImageView) to bounce off of a block (also a UIImageView). My ball switches both the x and y movements to negative instead of just one. What is wrong??? Please Help... Here is my code
-(void)animateBall:(NSTimer *)theTimer {
collisionCount ++;
bouncyBall.center = CGPointMake(bouncyBall.center.x + ballMovement.x, bouncyBall.center.y + ballMovement.y);
if (CGRectIntersectsRect(bouncyBall.frame, wallOne.frame)) {
if (collisionCount >= 5) {
[self processCollision:wallOne];
collisionCount = 0;
}
}
if (bouncyBall.center.x > 313 || bouncyBall.center.x < 8) {
ballMovement.x = -ballMovement.x;
}
if (bouncyBall.center.y > 453 || bouncyBall.center.y < 8) {
ballMovement.y = -ballMovement.y;
}
}
-(void)processCollision:(UIImageView *)wall {
if (ballMovement.x > 0 && wall.frame.origin.x - bouncyBall.center.x <= 4) {
ballMovement.x = -ballMovement.x;
}
else if (ballMovement.x < 0 && bouncyBall.center.x - (wall.frame.origin.x + wall.frame.size.width) <= 4) {
ballMovement.x = -ballMovement.x;
}
if (ballMovement.y > 0 && wall.frame.origin.y - bouncyBall.center.y <= 4) {
ballMovement.y = -ballMovement.y;
}
else if (ballMovement.y < 0 && bouncyBall.center.y - (wall.frame.origin.y + wall.frame.size.height) <= 4) {
ballMovement.y = -ballMovement.y;
}
}

Does it happen even when the ball hits the center of an edge? Anyway don't you just need to handle one dimension at a time? Can't the third if be else if?

Related

How to test if a function is called using open/wc testing with sinon?

I am new to web components, and am using LIT framework. I have been breaking my head trying to figure this out.
This is my function.
createObj() {
this.financingObj = [];
if (this.dropDownValue === common.ONCE) {
if (this.ammountEntered >= 2000 && this.ammountEntered <= 999999) {
this.financingObj.push(this.createFinancingStructure('BusinessLoan'));
}
if (this.ammountEntered >= 10000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('Leasing'));
}
if (this.ammountEntered >= 250000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('InvestmentLoan'));
}
if (this.ammountEntered >= 2500000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj = this.financingObj.filter(
item => item !== this.createFinancingStructure('BusinessLoan'),
);
this.financingObj.push(this.createFinancingStructure('RollOverTermLoan'));
}
} else if (this.dropDownValue === common.MULTIPLE_TIMES) {
if (this.ammountEntered >= 250000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(this.createFinancingStructure('InvestmentLoan'));
}
if (this.ammountEntered >= 2500000 && this.ammountEntered <= common.MAX_ALLOWED_VALUE) {
this.financingObj.push(
this.createFinancingStructure('RollOverTermLoan'),
this.createFinancingStructure('RollOverRevolving'),
);
}
}
}
And this is my test case
it('should call createObjForInvest when invest product is selected', () => {
const spy = sinon.spy(element, 'createObj');
spy();
sinon.assert.called(spy);
});
I see the function gets called, but I am getting the following error.

Kinect 2 with unity3d speed keep on when the player stopped but keep one knee up

I am making a running game that detect the player knee movement and add speed , its working fine but the problem is when I stop running and keep one knee up the game speed do not stop ,I need the speed to be decreased when I stop running even if one knee is still.
if (!player1)
{
player1 = GameObject.Find("player1");
player1State.text = "";
}
if (player1)
{
player1FootRight = player1.transform.Find("KneeRight").gameObject;
player1FootLeft = player1.transform.Find("KneeLeft").gameObject;
player1State.text = "OK";
player1State.color = Color.white;
newPosP1 = player1FootRight.transform.position.y;
oldPosP1 = player1FootLeft.transform.position.y;
if (startRunning) {
if (foot1 && newPosP1 > oldPosP1)
{
float k = newPosP1 - oldPosP1;
if (k > 1 && speed < 100)
{
speed = speed + 7;
if(leftPath.speed < 15)
{
leftPath.speed = leftPath.speed + 3;
}
footOld = foot1;
foot1 = !foot1;
}
else
{
if (speed > 0)
{
speed = speed - 1;
if (leftPath.speed > 0)
{
leftPath.speed = leftPath.speed - 1;
}
}
if (speed < 0)
{
speed = 0;
}
if (leftPath.speed < 0)
{
leftPath.speed = 0;
}
}
}
if (!foot1 && newPosP1 < oldPosP1)
{
float k = oldPosP1 - newPosP1;
if (k > 1 && speed < 100)
{
speed = speed + 7 ;
if (leftPath.speed < 15)
{
leftPath.speed = leftPath.speed + 3;
}
footOld = foot1;
foot1 = !foot1;
}
else
{
if (speed > 0)
{
speed = speed - 1;
if (leftPath.speed > 0)
{
leftPath.speed = leftPath.speed - 1;
}
}
if (speed < 0)
{
speed = 0;
}
if (leftPath.speed < 0)
{
leftPath.speed = 0;
}
}
}

JS Function Every 100 Points

I tried writing this on my own, but I failed miserably. Right now I have a script checking the score in my game. I check for every 100 points and change a variable depending on the score, but I'm doing it the wrong and tedious way:
if(gameController.speed == 1) {
if(score >= 200) {
squishyController.gravity = -25;
}
if(score >= 300) {
squishyController.gravity = -50;
}
if(score >= 400) {
squishyController.gravity = -75;
}
if(score >= 500) {
squishyController.gravity = -100;
gamePause = true;
// Spawn First Boss
}
if(score >= 600) {
squishyController.gravity = -125;
}
if(score >= 700) {
squishyController.gravity = -150;
}
}
You get the point. What I want to do in word form is this:
For every 100 points
Change gravity by -25
if the points are 500, 1000, etc
spawn boss
That's it in a nutshell. Here's what I'm thinking it would look like but don't kill me.
for (var i = 0; i += pointValue) {
// Check if points are being added then add to score depending on point value
if(getPoints == true) {
i++;
}
// For every 100 points change the gravity value
if(i >= incrementsOf100) {
gravity = gravity -= 25;
}
// For every 500 points, spawn a boss more difficult than the last
if(i = incrementsOf500) {
bossDifficulty = bossDifficulty += 100; // Just adding this because I will add more hitpoints to boss.
spawnBoss = true;
}
}
How about using the modus operator to find the remainder of dividing by 100, then divide the score minus that remainder to get the number of times you need to change gravity
var score = 550;
var extra = score % 100;
var gravityTimes = (score - extra) / 100;
gravity = gravity -= (25 * gravityTimes);

making a trail that destroys enemies in unity

After searching for a way to do this I found a script from robertbu that should work for detecting collisions, I am trying to make it so that when this system detects a collision with "Virus" by using other.CompareTag like you would with ontriggerenter(other:collider). Here's the neccesary part of the script.
function Update() {
if (Hit() == true){
if (other.CompareTag("Virus")){
Destroy(other.gameObject);
}
}
}
function Hit() : boolean {
var i = head;
var j = (head - 1);
if (j < 0) j = arv3.Length - 1;
while (j != head) {
if (Physics.Linecast(arv3[i], arv3[j], hit))
return true;
i = i - 1;
if (i < 0) i = arv3.Length - 1;
j = j - 1;
if (j < 0) j = arv3.Length - 1;
}
return false;
}
function Update() {
Hit();
}
function Hit() : boolean {
var i = head;
var j = (head - 1);
if (j < 0) j = arv3.Length - 1;
while (j != head) {
if (Physics.Linecast(arv3[i], arv3[j], hit) && hit.collider.tag == "Virus") {
Destroy(hit.collider.gameObject);
}
i = i - 1;
if (i < 0) i = arv3.Length - 1;
j = j - 1;
if (j < 0) j = arv3.Length - 1;
}
}
it works with this code

Change Value of Duplicate Object in NSMutableArray?

I have the method below, which in my Blackjack app will get the value of the hand which is an NSMutableArray. The problem here is that when 2 Ace's are in a hand, it should be a 12, but because it counts Ace's as 11, it results in being 22, which then makes lowValue returned.
How can I make it so that I can check and see if the for loop has already found an Ace and if it finds another, makes the next Ace worth only 1, not 11?
Thanks!
int getHandValue(NSMutableArray *hand) {
int lowValue = 0;
int highValue = 0;
for (KCCard *aCard in hand) {
if (aCard.value == Ace) {
lowValue+= 1;
highValue+= 11;
} else if (aCard.value == Jack || aCard.value == Queen || aCard.value == King) {
lowValue += 10;
highValue += 10;
} else {
lowValue += aCard.value;
highValue += aCard.value;
}
}
return (highValue > 21) ? lowValue : highValue;
}
Perhaps you can add a boolean value before the for loop setting it initially to NO. When an Ace is found then you can break from the for loop after setting the boolean to YES, that way if you encounter another Ace && your boolean value == YES you can handle the case accordingly.
int getHandValue(NSMutableArray *hand) {
int lowValue = 0;
int highValue = 0;
BOOL isFoundAce = NO;
for (KCCard *aCard in hand) {
if (aCard.value == Ace) {
if (isFoundAce) {
lowValue+= 1;
highValue+= 1;
}
else {
lowValue+= 1;
highValue+= 11;
isFoundAce= YES;
}
} else if (aCard.value == Jack || aCard.value == Queen || aCard.value == King) {
lowValue += 10;
highValue += 10;
} else {
lowValue += aCard.value;
highValue += aCard.value;
}
}
return (highValue > 21) ? lowValue : highValue;
}
My example without a redundant code from zsxwing's example:
int getHandValue(NSMutableArray *hand) {
int cardValue = 0;
int aceCount = 0;
for (KCCard *aCard in hand) {
if (aCard.value == Ace) {
aceCount++;
cardValue += 11;
} else if (aCard.value == Jack || aCard.value == Queen || aCard.value == King) {
cardValue += 10;
} else {
cardValue += aCard.value;
}
}
while ((cardValue > 21) && (aceCount > 0)) {
cardValue -= 10;
aceCount--;
}
return cardValue;
}