-1

I'm trying to create a brick breaker type game with a ball and bat. I'm working on making the ball reflect off the bat like it does with the walls but i the way i am trying it seems to be wrong or is something i do not understand very well:

private int x;
private int batx;
if (x = batx)

comes up with the message cannot implicitly convert type 'int' to 'bool'

i just started C# so i don't really understand what to do. is there another way i can make the ball reflect off the bat?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Mikhel Patel
  • 1
  • 1
  • 4
  • 3
    `if (x == batx)`. This is C# protecting you from one example of the silly typo-based errors C/C++ lets you make. :) – Peter Duniho Nov 18 '14 at 00:59
  • i see thank you very much. i just tried that and doing the same with y and bat y, but the ball just goes through the bat still. how do i make the ball and bat recognise each other? – Mikhel Patel Nov 18 '14 at 01:02
  • 2
    @MikhelPatel A question about collision detection between balls and bats is *pretty different* from a question about comparing two integers and the equals operator's difference from the assignment operator. – Preston Guillot Nov 18 '14 at 01:06
  • @MikhelPatel: you should post a new question if you have something new to ask. Don't piggy-back, not even if the first question turned out to be something incredibly simple. – Peter Duniho Nov 18 '14 at 01:07
  • @PrestonGuillot this was something that i wanted to try to see if it would somehow help. i thought that the x coordinate and y coordiante of the bat and ball were equal and i put in that code above, i thought it would work. i guess not :/ – Mikhel Patel Nov 18 '14 at 01:08
  • @PeterDuniho ok will do – Mikhel Patel Nov 18 '14 at 01:08

2 Answers2

4

ekad gave the correct answer here, but since this is an opportunity for learning:

if statements expect a bool value inside their parentheses, which is why you can have things like:

if (true)

and

if (false)

but

if (1)

doesn't make any sense. When you do a comparison (==) like:

if (a == b)

a is compared to b for equivalence, and the statement will evaluate to either true or false. This probably makes sense to you already.

Assignment (=), however, also evaluates to a value, and the value is the value of the left operand. The type returned is the type of the left operand.

batx = 5;
if (x = batx) {

essentially evaluates to

if (5)

Which, as said before, doesn't make sense (in C#, anyway -- this does make sense in C). The reason why I typed all that out is that it explains the compiler error you got.

cannot implicitly convert type 'int' to 'bool'

The compiler expected to find a statement which evaluates to a bool inside the parentheses. Instead, it found a statement that evaluates to int.

stromdotcom
  • 381
  • 1
  • 8
2

This is where your mistake is, you use = Operator in the condition of the if block

if (x = batx)

The condition (x = batx) should be a boolean, so you should use == Operator instead

if (x == batx)
ekad
  • 14,436
  • 26
  • 44
  • 46