Click here to Skip to main content
15,917,177 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I am using XNA2D which uses C#. I am new to C#
i am trying to move a group of objects around. So far, i am able to move only one object at a time.
Here is my code.
C#
if (input == null)
{
    throw new ArgumentNullException("input"");
}

mouseStateCurrent = Mouse.GetState();
drag = Vector2.Zero;

foreach (ALU a in ALU.List)
{
    if (a.Rectangle.Contains(mouseStatePrevious.X, mouseStatePrevious.Y) && 
        !dragging && 
        mouseStateCurrent.LeftButton == ButtonState.Pressed)
    {
        selectedALU = a;
        dragging = true;
        drag.X = selectedALU.Position.X - mouseStatePrevious.X;
        drag.Y = selectedALU.Position.Y - mouseStatePrevious.Y;
    }

    if (mouseStateCurrent.LeftButton == ButtonState.Released)
    {
        if (dragging)
        {
            selectedALU.Position.X = (float)((Math.Round(selectedALU.Position.X / 64)) * 64);
            selectedALU.Position.Y = (float)((Math.Round(selectedALU.Position.Y / 64)) * 64);
        }

        dragging = false;
    }
    mouseStatePrevious = mouseStateCurrent;
}
Posted
Updated 13-Jun-11 6:43am
v2
Comments
#realJSOP 13-Jun-11 12:43pm    
Fixed your code block formatting
Ed Nutting 13-Jun-11 14:03pm    
Can I calrify your question - what you want to do is move all the object in your ALU.List? If so, what you have seems like the only way. There is no generic "move group" method in XNA :P
adeolao 14-Jun-11 11:01am    
not necessarily move all the objects in the ALU.list but to move a selection of objects around using a selection box. I saw an example online but i couldn't seem to figure out what exactly they did because it was a pretty in depth game with alot of effects and also am new to c#.
milenalukic 14-Jun-11 16:14pm    
I suggest you post the sample you found maybe someone could explain it to you and alter for your needs.
Member 8043849 3-Jul-11 21:38pm    
ya u must post the link u found

1 solution

EdMan196 is correct there is no defacto way to move multiple objects "simultaniously".. that said....

There are a number of decent ways to approach this, the best IMO is if you are using a "subgrouping" approach you can map a temporary grouping from a centroid (say the cursor) and draw using the deltas from the mapping added to your cursor displacement.. this is probably the best approach as it will allow for the object to still have "behavior" on their own and lets you treat the "selection moved" vector as a perturbation rather then explicitly.

(using this approach you could get the objects to still move relative to one another inside your selection group if you so desired)

alternatively you could look at applying a transform, which might help clean up the approach from a code perspective but will be slightly slower in execution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900