我試圖將函數範圍外聲明的枚舉傳遞給函數,以便更改函數內的枚舉值可以更改指向的枚舉值。僅僅在對象範圍內使用枚舉本身不是一個選項,因爲我希望將此函數與此枚舉的多個不同實例一起使用。傳遞枚舉函數的指針
我有一個enum'ColourState'。聲明如此。和一個指向ColourState的指針。
enum ColourState {COLOUR1, COLOUR2};
ColourState* CS_GO_score;
指針初始化像這樣。
CS_GO_score = new ColourState;
*CS_GO_score = ColourState::COLOUR2;
我現在想通過ColourState指向CSS_GO_score給函數「pulsateColour」
像現在這樣。
void HUD::pulsateColour(GLfloat *colour1, GLfloat *colour2, GLfloat *objectCol, ColourState goingTo, int timeInFrames)
{
GLfloat difference[4];
//give us an array of values to change the array by in the alloted time
difference[0] = (colour1[0]-colour2[0])/timeInFrames;
difference[1] = (colour1[1]-colour2[1])/timeInFrames;
difference[2] = (colour1[2]-colour2[2])/timeInFrames;
difference[3] = (colour1[3]-colour2[3])/timeInFrames;
//depending on the state, transform the array in one direction or another
if(goingTo == ColourState::COLOUR2)//if we're moving toward colour 2
{
for(int i = 0; i<4; i++)
{
objectCol[i] -= difference[i];//subract the difference till we get there
//we need to SNAP to the colour as we will not hit it every time using floats
if((objectCol[i]>(colour2[i]-(difference[i]*2))) && (objectCol[i]<(colour2[i]+(difference[i]*2))) )
{//if we hit this tiny but huuuge target
objectCol[i] = colour2[i];//SNAP
}
}
}else{
if(goingTo == ColourState::COLOUR1)
{
for(int i = 0; i<4; i++)
{
objectCol[i] += difference[i];//add the difference till we get there
//we need to SNAP to the colour as we will not hit it every time using floats
if((objectCol[i]>(colour1[i]-(difference[i]*2))) || (objectCol[i]<(colour1[i]+(difference[i]*2))) )
{//if we hit this tiny but huuuge target
objectCol[i] = colour1[i];//SNAP
}
}
}
}
if((objectCol[0] == colour1[0])&&(objectCol[1] == colour1[1])&&(objectCol[2] == colour1[2])&&(objectCol[3] == colour1[3]))
{//if the objcolour == colour 1
goingTo = ColourState::COLOUR2;//it's now time to move towards colour 2
}else{
if((objectCol[0] == colour2[0])&&(objectCol[1] == colour2[1])&&(objectCol[2] == colour2[2])&&(objectCol[3] == colour2[3]))
{//if the objcolour == colour 2
goingTo = ColourState::COLOUR1;//it's now time to move towards colour1
}
}
}
}
該功能被稱爲像這樣。
pulsateColour(white,blue, GO_scoreColour, *CS_GO_score, 10);
然而,當值指針到「打算去」和「CS_GO_score」(他們應該是相同的地址,因此在技術上同一個對象吧?),看值VS監視我看到只有值'goingTo'指向(因爲它是本地的功能)改變了?我究竟做錯了什麼?
所以你是一個反恐精英傢伙;)感謝您的問題,幫助我! – 2016-10-10 09:34:03