HI,
I have this:
[code]
private Bitmap InvaderImage(int animationCell)
// public void DisplayImage()
{
switch(InvaderType)
{
case Type.Bug:
switch(animationCell)
{
case 0: StarInvader2.Properties.Resources.bug1;
break;
case 1:Properties.Resources.bug2;
break;
case 2: Properties.Resources.bug3;
break;
case 3: Properties.Resources.bug4;
break;
default:
break;
}
}
return image;
}//End method
[/code]
But then I get this error:
Error 3 Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\Users\savantKing\Desktop\StarInvader2\Invader.cs 139 29 StarInvader2
Loading
VulpesPosted Mar 27, 2011, 10:15 AM
private Bitmap InvaderImage(int animationCell)
// public void DisplayImage()
{
switch(InvaderType)
{
case Type.Bug:
switch(animationCell)
{
case 0:
return Properties.Resources.bug1;
case 1:
return Properties.Resources.bug2;
case 2:
return Properties.Resources.bug3;
case 3:
return Properties.Resources.bug4;
default:
break;
}
break;
case Type.satelite:
switch (animationCell)
{
case 0:
return Properties.Resources.flyingsaucer1;
case 1:
return Properties.Resources.flyingsaucer2;
case 2:
return Properties.Resources.flyingsaucer3;
case 3:
return Properties.Resources.flyingsaucer4;
default:
break;
}
break;
}
return image;
}//End method
Sam HobbsPosted Mar 27, 2011, 5:36 PM
Alternatively, you could put the images into an array and use the types as indexes into the array(s). I am sorry that I have not studied the code closely enough, but it appears that you can use a two-dimensional array. I think Vulpes will agree and Vulpes would have suggested this too except he answered the question as it was asked.
Suthish NairPosted Mar 27, 2011, 1:28 PM
albert albertPosted Mar 27, 2011, 10:23 AM
THX man!!
Solved.
albert albertPosted Mar 27, 2011, 10:11 AM
I still get some problems.
But I now have this:
[code]
private Bitmap InvaderImage(int animationCell)
// public void DisplayImage()
{
switch(InvaderType)
{
case Type.Bug:
switch(animationCell)
{
case 0:
return Properties.Resources.bug1;
case 1:
return Properties.Resources.bug2;
case 2:
return Properties.Resources.bug3;
case 3:
return Properties.Resources.bug4;
default:
break;
}
case Type.satelite:
switch (animationCell)
{
case 0:
return Properties.Resources.flyingsaucer1;
case 1:
return Properties.Resources.flyingsaucer2;
case 2:
return Properties.Resources.flyingsaucer3;
case 3:
return Properties.Resources.flyingsaucer4;
default:
break;
}
}
return image;
}//End method
[/code]
I think this make it more clear.
But then I get this error:
Error 6 Control cannot fall through from one case label ('case 0:') to another C:\Users\savantKing\Desktop\StarInvader2\Invader.cs 135 17 StarInvader2
How to solve this?
albert albertPosted Mar 27, 2011, 9:56 AM
u are right!!
I just forgot the return!!
oh, so stupid of me!!
iv solved.
THX!!
albert albertPosted Mar 27, 2011, 9:50 AM
but how u get the right type then?
This is the hole class Invader:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace StarInvader2
{
[Serializable]
public class Invader
{
//InvaderType invaderType;
Form1 form1;
public Boolean hit = false;
Game game;
public Bitmap image;
public Point Location;
//public Point Location { get; private set; }
public Type InvaderType { get; private set; }
public Bitmap[][] InvaderImageBlock;
public Rectangle Area
{
get { return new Rectangle(Location, image.Size); }
}
public int Score { get; private set; }
public Invader(Type invaderType, Point location, int score)
{
//this.image = StarInvader2.Properties.Resources.bug1;
this.InvaderType = invaderType;
this.Location = location;
this.Score = score;
this.Score = GameConstants.BugScore;
this.Score = GameConstants.SatelliteScore;
this.Score = GameConstants.SaucerScore;
this.Score = GameConstants.SpaceshipScore;
this.Score = GameConstants.StarScore;
//image = InvaderImageBlock(0);
SetupImage();
//this.image = InvaderImageBlock[(int)invaderType][0]; //Object is null.
this.image = InvaderImage(0);
}//End constructor.
public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
{
Bitmap bitmap = new Bitmap(ImageToResize);
using (Graphics graphics = Graphics.FromImage(bitmap))//Draw FromImage
{
graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
}
return bitmap;
}//
private Bitmap InvaderImage(int animationCell)
// public void DisplayImage()
{
switch(InvaderType)
{
case Type.Bug:
switch(animationCell)
{
case 0:
Properties.Resources.bug1;
break;
case 1:Properties.Resources.bug2;
break;
case 2: Properties.Resources.bug3;
break;
case 3: Properties.Resources.bug4;
break;
default:
break;
}
}
return image;
}//End method
public void Draw(Graphics g, int animationCell)
{
g.DrawImage(this.image, this.Location);
}//End method
public void Move(Direction direction)
{
switch (direction)
{
case Direction.Down:
Location.Y += GameConstants.VerticalInterval;
break;
case Direction.Left:
Location.X -= GameConstants.HorizontalInterval;
break;
case Direction.Right:
Location.X += GameConstants.HorizontalInterval;
break;
case Direction.DownLeft:
Location.Y = GameConstants.VerticalInterval;
//Location.X -= GameConstants.HorizontalInterval;
break;
default: break;
}
} //End method
//}
//return image;
//}//End method
public void ScoreInvader()
{
switch (Score)
{
case Type.Bug:
GameConstants.BugScore;
break;
}
}//End method
public void UpdateInvaderImage(int animationCell)
{
image = InvaderImageBlock[(int)this.InvaderType][animationCell];
}
}
}
[/code]
It has to be a double swith?
Every type has 4 pictures. For example: bug1, bug2, bug3, bug4
VulpesPosted Mar 27, 2011, 9:45 AM
albert albertPosted Mar 27, 2011, 9:35 AM
I have a enum:
Type:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarInvader2
{
public enum Type
{
Bug, //= 0,
Saucer, //= 1,
satelite, //= 2,
Spaceship,// = 3,
star //= 4
}
}
[/code]
And so I want to make animation of a type, for example: Type.Bug And I have 4 pictures of eatch type.
so I use 2 switch: 1 for the type and 1 for the image.
Properties.Resources.bug1; = is a picture
But u can do a nested switch statment or not?
VulpesPosted Mar 27, 2011, 9:23 AM
StarInvader2.Properties.Resources.bug1
Properties.Resources.bug2
Properties.Resources.bug3
Properties.Resources.bug4
What are you trying to do here - label the case statements in some way? If so just use comments.
Or, if they're string properties (and animationCell is also a string) and you want to switch on the strings, you can do:
switch(animationCell)
{
case StarInvader2.Properties.Resources.bug1:
break;
case Properties.Resources.bug2:
break;
case Properties.Resources.bug3:
break;
case Properties.Resources.bug4:
break;
default:
break;
}