Skip to main content

C# How to spawn Enemies and move them? [closed]

I am trying to spawn enemies and move them from left to right with a timer how can i do this?

Example:

int count;
timer (every 1 sec)
{
    Enemy somename+count = new Enemy();
    count++;
}

timer (every 0.001 sec)
{
    somename+count.x++;
}

Solved

Your fundamental problem seems to be lacking a way to store references to all of your enemies. Generics and collections provide a solution to this type of problem. I'll use a List, which is the most basic such collection. There are many others, however, that each have their own useful properties.

Consider :

private class Enemy
{
    public int position;
}

private List enemyList = new List();

private void timer1_Tick(object sender, EventArgs e)
{
    enemyList.Add(new Enemy());  
}

private void timer2_Tick(object sender, EventArgs e)
{
    foreach(Enemy enemy in enemyList)
    {
        enemy.position++;
    }
}

Also consider that updating a large list of visual objects every 0.001 second is useless and wasteful. One millisecond updates correspond to 1000fps. No video device will update that fast and no human being can see that fast. Generally, for real-time updates you should aim to update between 30-60fps. That corresponds to no more frequently than about 0.016s (16ms).


If this is an assignment, and to get it up and running. Just use the Timer class. You will not get as high resolution as 1000hz (1ms) as you wished for the movement though.

Documentation: Timer class

        var timerCreate = new Timer {Interval = 1000};
        timerCreate.Tick += delegate(object o, EventArgs args)
        {
            // create them 
        };

        var timerAction = new Timer {Interval = 50};
        timerAction.Tick += delegate(object o, EventArgs args)
        {
            // move them            
        };

As you are creating, the enemies inside a local scope, you cannot access them, once you get out of the loop. Instead, have a list and add your newly spawned enemies to this list. Maintain two threads for doing your two tasks. 1) Thread - 1 will add spawn enemies add adds enemies to our list. 2) Thread -2 will move the enemies in our spawned list. But, as out list is a shared resource between both the threads, always access the list using 'locks'.


Comments

Popular posts from this blog

Android : MPAndroidChart how to remove values on right side in chart?

I have the following chart which is displaying some values. I would like to hide/ remove values on the right side, because on the left side are enough. See image below: And code is following: public class MainActivity extends AppCompatActivity implements OnChartValueSelectedListener{ private LineChart mDataLineChart; private RelativeLayout mRelativeLayout; private LineChart mChart; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_linechart); // Set chart setChart(); // add data setData2(3, 155); // ANIMATE CHART animateChart(); } private void setChart() { mChart = (LineChart) findViewById(R.id.chart1); mChart.setOnChartValueSelectedListener(...

Can't seem to get my method (in Java) to compile correctly

I am trying to figure out exactly what is wrong with my winorTie method in this little tictacttoe game I am trying to create. Would anyone be able to help? Thanks package tictactoegame; /** * * @author Douglas Boulden */ public class tictactoegame { static int [][] gameboard; static final int EMPTY = 0; static final int NOUGHT = -1; static final int CROSS = 1; static void set (int val, int row) throws IllegalArgumentException { int col = 0; if (gameboard[row][col] == EMPTY) gameboard[row][col] = val; else throw new IllegalArgumentException("Player already there!"); } static void displayBoard () { for (int[] gameboard1 : gameboard) { System.out.print("|"); for (int c = 0; c Solved Some of this was mentioned in the comments, but these conditions fundamentally don't make sense: if (gameboard [0][0] == NOUGHT && gameboard [0][...